Conditional Step in Jenkins CI

Conditional Step in Your Continuous Delivery Pipeline

Continuous Delivery (CI)  often requires something to happen under certain condition. You can have different pipelines, jobs or tasks dedicated to differentiate it, but as far as best practices states – reuse. In my experience of building Delivery Pipelines, it starts with single pipeline for a product, but is often configured to have multiple paths depending on options chosen at the first step.

My conditional step

In my case I was adopting Delivery Pipeline to run tests depending on “SKIP TESTS” option when starting pipeline build. For this I used “Parametrised Build” option which is often provided by default or can be added as a plugin for Jenkins. I assume you already know how (as this is super simple) to install plugin in Jenkins, so I will skip this part. After successfully installing plugin, new option will appear in job configuration step under the name “This build is parameterized”.

Conditional Step in Jenkins CI

This will allow to add various options to customise your jobs. In my case I wanted to “Fast-forward” urgent builds so I needed make it as fast as possible. For that I chose conditional step to skip testing certain long-running tests. I added build parameter to the first job of the build pipeline as below.

Conditional Step in Jenkins CI

If you need to decide on something right in the same job, you can simply use it as environment variable in “bash shell” build step.

In the example above, I added #!/bin/bash in order to define conditions simpler, otherwise each line are executed separately, with required either to define condition in a single line either to do some bash magic.

When you need to make a decision in job further the build pipeline, you can simply pass this environment variable. To pass it along, you can simple use “Current build parameters” as parameter to pass when triggering next pipeline job.

Conditional Step in Jenkins CI - Pass environment parameters along build pipeline

Complex conditional step

Now right to the code of this article. As you may imagine, sometimes build pipeline required really complicated flow to be flexible and not repetitive. Conditional BuildStep Plugin adds a way to nicely define it in an understandable and visual way. In our case I will use it to dynamically switch between jobs depending on option chosen when starting build pipeline. Plugin allows two kind of actions to take: single or multiple steps.

Conditional Step in Jenkins CI - Single and multiple steps

As I have single checkbox option, I will be choosing “Boolean condition” step.

Conditional Step in Jenkins CI

In order to make tests optional, I added ${SKIP_TESTS} to the token field. Curly braces and dollar sign required to evaluate actual environment variable, not a string itself.

Conditional Step in Jenkins CI - Boolean conditional step

Now depending on chosen option, it will either execute or not. You can choose from wide options how to react to your build parameters: start other jobs, execute shell or other, provided by many other plugins.

Conclusion

This is really nice plugin for agile pipeline, though it adds a bit of complexity to it, but much less than having tons of bash scripts to decide what to do next. Visualising always help! So I encourage you to experiment with it and post me your ideas and thoughts on it.

C U,
ME

4 comments

  1. Wonderful article, very useful and well explanation. Your post is extremely incredible.
    <a href=”http://spunksoft.com/course/rpa-training-in-hyderabad/”>
    RPA Training in Hyderabad
    </a>

  2. It is very good and very informative. There is a useful information in it.Thanks for posting… <a href=”https://kerneltraining.com/course/hyderabad/devops-training-in-hyderabad/”>DevOps Training In Hyderabad</a>

Leave a Reply

Your email address will not be published. Required fields are marked *