Development

How to use Jenkins allOf Operator - Declarative Pipeline Syntax

This tutorial will cover the basics of using the allOf operator in a Jenkins when expression.
Captain Salem 2 min read
How to use Jenkins allOf Operator - Declarative Pipeline Syntax

We can use the when expression in a Jenkinsfile to specify the conditions under which a particular build step or post-build action should be executed. It allows you to specify a Boolean expression that determines whether the build step or post-build action should be run.

Jenkins When Expression

The when expression must contain at least one Boolean condition. You can also combine multiple conditions, but all the specified conditions must evaluate true for the block to run.

The following shows the syntax of the when expression in Jenkins.

when {
   <condition>
 }

In this case, the condition specifies the Boolean expression that is evaluated before running or not running the build step or post-build action,

There are several types of conditions that you can use in the when expression, including:

  • Branch - specifies a branch name or a regular expression that is used to match against the current branch
  • expression - specifies a Groovy expression that is evaluated to determine whether the build step or post-build action should be run
  • not - negates the condition that follows it.
  • allOf - specifies that all of the conditions that follow it must be true for the build step or post-build action to be run.
  • anyOf: specifies that at least one of the conditions that follow it must be true for the build step or post-build action to be run

Let us look at an example pipeline on how we can use the when expression in Jenkins.

Jenkins allOf Operator

We can use the allOf operator in a when clause to specify that a build should be executed only if all specified conditions are met. An example demonstration is as shown below:

pipeline {
   agent any
   stages {
     stage('Build') {
       when {
         allOf {
           branch 'master'
           environment name: 'BUILD_ENV', value: 'prod'
         }
       }
       steps {
         echo "Build step executed"
       }
     }
   }
 }

In the example above, Jenkins will only execute the Build stage if the current branch is master and the BUILD_ENV environment variable is set to prod. If neither of the specified conditions is true, the entire stage will be skipped.

Ending

This short tutorial taught us how to use the allOf operator in a Jenkins when clause. The allOf operator allows us to nest a series of conditionals and only execute a given step when all defined conditions are true.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.