{{announcement.body}}
{{announcement.title}}

How to a Start Jenkins Build Using the Last Successful Build on a Test Environment

DZone 's Guide to

How to a Start Jenkins Build Using the Last Successful Build on a Test Environment

Take a look at how you can obtain and use the commit ID of a previous build to begin another.

· DevOps Zone ·
Free Resource

Image title


Last week I faced with a not-so-trivial case in CI practice. There is production build running once a day by schedule and it uses a master branch as a source. The main challenge here that the development team is not using branches in everyday practice. (Why? That is quite another topic, but at the moment we stay with one branch.)

Such a setup was fine until dev team size hit four engineers. It often becomes the case that the Jenkins build starts while in the master branch when we have some unfinished task that is in progress. It becomes obvious that we need to use it for the production build, but only the commit that produces the last successful build on the stage environment. Sounds good and should quite easy to implement, but...

Jenkins is great if you have a pipeline of builds (one build triggers another one right after successful completion). However, it is not so easy to push a scheduled job to check the state of another job and obtain the commit ID in pre-built actions. At least, I do not find anything out of the box.

Let's try to create it on our own. First of all, we need to obtain the last successful build with all the additional info. Checking of Jenkins API give us a link to do it: http://<jenkins_url>/job/<job_name>/lastSuccessfulBuild/api/json.

It will produce a huge JSON output, but the most intersing part is here:

"lastBuiltRevision":{"SHA1":"<commit_hash>"}


We could reduce the JSON size to pass the exact names of the fields and depth of search over JSON: 

http://<jenkins_url>/job/<job_name>/lastSuccessfulBuild/api/json?tree=actions[lastBuiltRevision[SHA1]]&depth=

OK, commit hash can be obtained.

Now, we need to trigger this API call during pre-build actions, parse the commit hash, and use it for build start. It can be done with Jenkins EnvInject Plugin. Set up the built-in parameter and you should enable the option Prepare an environment for the run.

Ok, now we have an opportunity to override the environment variables before the build starts and can use  hardcode values, script (not sure what kind of script can be used, because I find out next option), and the Groovy script for this purpose. Groovy is a perfect language for me for such kind of automation, so I start using this option. What else do we need? We can override the env  variable, but what variable to override it with? Let's create one.

In the build parameters, you should enable the option This project is parameterized. Add the Git parameter section. Now name the parameter (I have used COMMIT_ID) and set the default value of the parameter (in my case, it was a reference to master's head).


Let's inject a new environment variable. It can be done in the section Source Code Management. Put a reference to the env  variable in a field Branch Specifier (blank for 'any').

And the last step: perform an API call, parse the hash and put it in the env  variable COMMIT_ID. Let's return to the Jenkins EnvInject Plugin and find the section Groovy script. Put in the field the script:

def post = new URL("http://<jenkins_url>/job/<job-name_to_check>/lastSuccessfulBuild/api/json?tree=actions[lastBuiltRevision[SHA1]]&depth=3").openConnection() String user = "<user>" String pass = "<API token>" String authStr = user +":"+ pass String encoding = authStr.getBytes("utf-8").encodeBase64().toString() post.setRequestMethod("POST") post.setDoOutput(true) post.setRequestProperty("Authorization", "Basic " + encoding) def postRC = post.getResponseCode() if(postRC.equals(200)) { def result = post.getInputStream().getText() def shaBegin = result.indexOf('SHA1":"') + 7 def shaEnd = result.indexOf('"', shaBegin) println(result.substring(shaBegin, shaEnd)) return ["COMMIT_ID": result.substring(shaBegin, shaEnd)] } 

The script is pretty straightforward. You need to pass your username and API token (click on your username in Jenkins at the top -> Configure -> Show Legacy API Token). Next, we just parse the hash from the resultant string and return to the map with the parameter name COMMIT_ID and hash value.

Topics:
ci ,ci pipeline ,commit id ,devops ,jenkins ,tutorial

Published at DZone with permission of Ivan Zerin , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}