A Beginner’s Guide to Jenkins — Conditionals

Ensuring that each stage runs exactly when and where it should

routerhan
2 min readSep 20, 2023

In this article, we delve into one such feature: conditional expressions in Jenkinsfiles.

Conditional expressions allow you to control when specific stages in your Jenkins pipeline run. It’s a game-changer for optimizing your Jenkins pipeline.

In this article, we’ll guide you through this feature step by step, making it easy to grasp. Let’s dive in!

Defining Conditional Expressions

Conditional expressions in Jenkinsfiles provide a powerful way to control when specific stages should execute.

Imagine scenarios like running tests exclusively on the development branch or triggering a build only when code changes occur.

To define conditional expressions, you use the “when” block within a stage. The syntax is straightforward:

stage('Example Stage') {
when {
expression {
// Your boolean expression here
}
}
steps {
// Define your stage steps
}
}

Within the “expression” block, you can create boolean expressions based on factors like the active branch.

--

--

routerhan

R&D Software Engineer @IBM Germany. This blog is an independent research of cloud computing.