Over a million developers have joined DZone.

Conditional BuildStep Jenkins Plugin for Improving Continuous Delivery Decisions

DZone's Guide to

Conditional BuildStep Jenkins Plugin for Improving Continuous Delivery Decisions

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

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.

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs


Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}