DevOps Zone is brought to you in partnership with:

Alex Soto discovered Java during second year of his studies in computer science and wrote his Master's Thesis on Emergent Intelligence applied to Data Mining, winning the second prize of Hermano Salvador González contest. During his 11 years of software development, Alex has worked in several companies as technical consultant, and currently is working as technical analyst at a major international pharmaceutical company developing in-vitro medical software meeting FDA standards, where performance and safety is "a must". Alex also works as tutor at Open University of Catalonia (UOC), teaching Compilers course. He spends his free time to write in his blog (http://www.lordofthejars.com) where some posts are published in sites like dzone, theserverside and springsource. Alex has spoken at Devoxx 2011 and Linuxtag 2012. Currently is developing NoSQLUnit, a JUnit extension for testing NoSQL systems. Alex is a DZone MVB and is not an employee of DZone and has posted 30 posts at DZone. You can read more from them at their website. View Full User Profile

Conditional BuildStep Jenkins Plugin for Improving Continuous Delivery Decisions

02.13.2013
| 3462 views |
  • submit to reddit

In Continuous Delivery each build is potentially shippable. This fact implies among a lot of other things, to assign a none snapshot version to your components as fast as possible so you can refer them through all the process.
Usually automated software delivery process consist of several stages like Commit stage, Code Quality, Acceptance Tests, Manual Test, Deployment, ... But let's focusing on the first stage. 
The first stage can contain next steps:
  • create a release branch
  • assign a version to the project
  • compile + testing
  • packaging (create a war, jar, ...)
But during the execution of one of these steps, a fail may occurs, for example code does not compile or some tests does not pass, and in this case we should delete the created release branch and stop the pipeline execution. On the other hand, if this stage ends successfully, Jenkins should run the next stage defined in pipeline.

To take this decision we are going to use Conditional BuildStep plugin, which as its name suggests, it allows us to choose which actions to fire depending on result of current job.

So after plugin is installed, we can create a job for the first stage:

So first of all, let's add a new build step (Execute Windows batch command or Execute shell) and launch a git command for creating a branch:

git checkout -b helloworld-release-%VERSION_NUMBER%.%BUILD_NUMBER%

In this case we are creating a branch with name of the project, the version number and finally the build number (which is provided by Jenkins).

Next step is to change the version of pom files (keep in mind that we are changing poms of branched project, not the "master") to current version. So let's create a new build step of type "Invoke top-level Maven targets" to invoke Versions Maven plugin.

versions:set -DnewVersion=%VERSION_NUMBER%.%BUILD_NUMBER%

Then we can call the clean install goals in a new build step.

clean install

Now is where Conditional BuildStep plugin starts to play:

If current build status is success, which means that code has been compiled and all tests pass, then we must commit the changes and pushing them to remote SCMserver.

So let's create a new build step called Conditional Step (single), and set that if build status is successful, git commands are executed.




But if build fails, the branch should be removed, so let's create a new conditional buildstep in the same build job, which will remove the created branch from current location:



We have learned how to use Conditional BuildStep plugin to take decisions depending on the status of the current build.

Published at DZone with permission of Alex Soto, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)