This Article is about using Jenksins REST API for getting information about the last successful build. Jenkins has no official Java/Kotlin API. This means, that we either have to use some third party Github developers java API, or just to use Jenkins REST API, that is guaranteed to work.
I have a Jenkins Instance, on which different jobs are running. I need the git hash of the last successful build for another project.
What can I do? I can go to Jenkins, scroll down to the last successful build of a branch and click on it. The git hash is under Revision and only needs to be copied from there. If you need to do that 20 times a day, it can be pretty nerve wracking. Another usecase for this would be, that you want to automate something that requires the last successful build hash and you can’t put that in manually every time.
As I said, there are at least two ways to solve this problem. For me, the safety that the solution is reliable and has a big community backing it off, was really important. I decided to not use the unofficial Java API and instead stick with the REST API calls. If the Java API gets official support at the time of the reading, make sure to check that out too.
Now let’s start with the configuration of the build.gradle.
Add this dependency to the build.gradle file:
Create a class called Jenkins, which has two functions.
Generate a Response
- import all the necessary dependencies from khttp.
- create a simple get request with your jenkins-server-address and job and branchName.
- The last part of this url must be /lastSuccessfulBuild.
- Create a Basic Authorization with a Jenkins Username and Password.
Get the last Successful Build
- Replace the “/” with “%252F” (since this is a special caracter in a url)
- Create the response by calling the private method defined before
- Get the substring out of the HTML if the statusCode is 200. (This substring is the git hash.)
- If the statusCode is not 200, return an error message
How to test it
Create a new Testclass with Junit.
To test the functionality of this class, we need to mock a new Response. I used Mockk for this, which is basically just Mockito for Kotlin. Since we only check the statusCode and the text of the response, so we only have to define the behaviour for them.
Make sure you’ve added the correct khttp imports for the Response.
Next, we need to create a spy of our class with spyk and define the behavour of our private method. Since this is a private method, we cant use it like we’re used to. We need to call it like this:
Then we create a hash variable which is just the output of the getLastSuccessfulBuild function.
The last step is to use assertThat for comparing the hash to the desired value.
Test the Error message
This is practically the same as the test before, with the difference that you change the statusCode to something other than 200.
Now instead of asserting a hash, you need to assert the error message with the right job and branchname.
Would I do this the same way next time?
Yes, kind of. I think I used the right components for this project. The only thing that I would change next time is to first read the documentation of mockk a little more. I had some problems with mocking the private function. At the beginning i didn’t even knew that this was possible, but it is documented pretty well in the documentation.
What went good?
The calls to the REST API worked pretty good right at the beginning. The mocking worked also pretty well with the exception of that private function.
What needs improvement?
Next time i would research a little more about mocking special methods and read more of the documentation itself.
I hope this article could help you 😺