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.
Join the DZone community and get the full member experience.
Join For Free
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.
Published at DZone with permission of Ivan Zerin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments