martes, febrero 12, 2013

Conditional BuildStep Jenkins Plugin for Improving Continuous Delivery decisions.

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 SCM server.

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 build step 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.

We keep learning,

PD: Although I am using this approach successfully in my projects, it is based on John Smart presentation.
The bugle sounds, the charge begins, But on this battlefield, no one wins, The smell of acrid smoke and horses' breath, As I plunge on into certain death, oh, oh (The Trooper - Iron Maiden)

Donate If You Can and Find Post Useful