DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • The Missing Link in Cybersecurity: Culture
  • Optimizing Firmware: The Key to Shipping IoT Devices on Time

Trending

  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • How to Perform Custom Error Handling With ANTLR
  • Secure by Design: Modernizing Authentication With Centralized Access and Adaptive Signals
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to a Start Jenkins Build Using the Last Successful Build on a Test Environment

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.

By 
Ivan Zerin user avatar
Ivan Zerin
·
Jun. 14, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
32.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Build (game engine) Jenkins (software) Testing

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

Opinions expressed by DZone contributors are their own.

Related

  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • The Missing Link in Cybersecurity: Culture
  • Optimizing Firmware: The Key to Shipping IoT Devices on Time

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: