💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Create Conditional Build Steps in Jenkins?

conditional-buildstep -
Jenkins Plugin
v.1.4.3
16
Required Jenkins: v.2.440.3
ID: conditional-buildstep
Contributors
Contributor - imodContributor - MarkEWaite
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Installation with the CLI tool
jenkins-plugin-cli --plugins conditional-buildstep:1.4.3

conditional-buildstep-plugin logo

Conditional Buildstep Plugin

Jenkins plugin that allows conditions to controls step execution


What is Conditional Buildstep Plugin?

This feature allows us to wrap and control the execution of multiple build steps based on a specific condition. It's a great tool for optimizing our job configurations and minimizing redundancy.

First off, we'll need to have both the TokenMacro Plugin and the Run Condition Plugin installed, as these are required for setting up the conditional build steps.

The idea here is pretty straightforward. Instead of having multiple jobs that do nearly the same thing, we can add a conditional buildstep. This acts like a container where you can define a condition that controls whether the included steps should execute or not.

By adopting this approach, we can streamline our processes, making our builds smarter and more efficient. It’s quite useful for maintaining clarity in our pipeline and reducing clutter. Plus, it gives us better control over when and why certain steps are executed, which I believe could be incredibly beneficial for our project management and execution.

What Types of Conditions Can Be Managed in Conditional BuildSteps?

Enhancing our pipeline flexibility with Conditional BuildSteps, which are controlled by various conditions defined by the Run Condition Plugin. Here’s a breakdown of some key features and how we can use them:

  1. Missing Builder Issue: Sometimes, you might not find the builder you want in the conditional build step dropdown. This usually means the builder doesn't have a @DataBoundConstructor or its Descriptor doesn’t extend hudson.tasks.BuildStepDescriptor. For those of us not deep into programming – it simply means the plugin hasn’t been updated to comply with the latest Jenkins coding standards. Without these updates, our conditional buildstep plugin won’t be able to interact with it.

  2. Conditional Step (Single): This build step lets you choose any single build step and apply a condition to decide if it should execute. It’s quite handy for streamlining processes.

  3. Freedom to Re-order: In the single-step setup, steps can be reordered independently, unlike in the multiple-step scenario where reordering is restricted to within the steps of that group. This feature also allows adding new steps in between existing ones, which could either always run or operate under a different condition than the adjacent steps.

  4. Moving Steps Between Conditions: If we’re using two conditions (via EnvInject) to control several steps, it’s easy to switch a single step to the other condition. In the multiple-step setup, all steps share the same condition, so changing conditions might require deleting and then re-adding the step under the new condition.

  5. Multiple Steps Under a Single Condition (EnvInject): We can configure multiple build steps to operate based on a single condition evaluation. By setting up a 'Inject environment variables' action via the EnvInject Plugin and defining a variable in the 'Properties Content', like CONDITION_X=y, all subsequent steps can be controlled with this condition using ${ENV,var="CONDITION_X"}.

These features provide us substantial control over our build process, allowing for more tailored and efficient workflows. It would be great to hear your thoughts on integrating these into our current system!