Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do simple if-statements inside a declarative pipeline in Jenkins

I'm trying to convert my Scripted pipeline to a Declarative Pipeline. Wondering how to do a simple if-statement inside a steps {} block.

    stage ('Deploy to Docker') {         steps {             parallel (                 "instance1" : {                     environment {                         containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim()                     }                     steps {                         if (containerId.isEmpty()) {                             docker.image('some/image').run("--name ${fullDockerImageName}")                         }                     }                 }             )         }    } 

This causes the following Exception:

WorkflowScript: 201: Expected a step @ line 201, column 29.                            if (containerId.isEmpty()) { 

Since I'm not allowed to do a simple if(..) inside a steps {} block, any idea on how to do this?

It doesn't seem to make sense to make this a full stage with a when {}, since there are more steps that happens in a simple stage (starting a stopped container if it exists, etc).

What's the best way to do a simple if?

like image 684
Jasper Roel Avatar asked Feb 16 '17 17:02

Jasper Roel


People also ask

Can we use if else in Jenkins pipeline?

Jenkins pipeline multiple if statement In Jenkins declarative/scripted pipeline, we can define multiple if-else blocks and use them in the Pipeline as per the use case. Below is the code snipped, which a user can use to define the multiple if-else blocks in Jenkins. sh "echo 'Hello from ${env. BRANCH_NAME} branch!

How do you use parameters in Jenkins declarative pipeline?

You just have to use params. [NAME] in places where you need to substitute the parameter. Here is an example of a stage that will be executed based on the condition that we get from the choice parameter. The parameter name is ENVIRONMENT , and we access it in the stage as params.

Can we run a step conditionally in Jenkins?

The Conditional BuildStep plugin lets users add conditional logic to Freestyle jobs from within the Jenkins web UI. It does this by: Adding two types of Conditional BuildStep ("Single" and "Multiple") - these build steps contain one or more other build steps to be run when the configured condition is met.

How to create a declarative style pipeline in Jenkins?

Step 1: Open Jenkins home page ( http://localhost:8080 in local) & click on New Item from the left side menu. Step 2: Enter Jenkins job name & choose the style as Pipeline & click OK. Step 3: Scroll down to the Pipeline section & copy-paste your first Declarative style Pipeline code from below to the script textbox.

Can declarative pipelines use all the available pipeline steps?

As it says here; Declarative Pipelines may use all the available steps documented in the Pipeline Steps reference, which contains a comprehensive list of steps, with the addition of the steps listed below which are only supported in Declarative Pipeline.

How to generate parameter pipeline code block in Jenkins?

You can generate the parameter pipeline code block easily using the Jenkins pipeline generator. You will find the Pipeline syntax generator link under all the pipeline jobs, as shown in the image below. Navigate to the pipeline generator in Jenkins and under steps, search for properties, as shown below. Using Parameters in Jenkinsfile

Can I use IF-THEN-ELSE inside a Jenkins script block?

So stay with the if-then-else inside a script block. Rationale: The idea of the declarative pipeline is to keep it simple without complex algorithms in it. When you are tempted to use if statements, loops, etc. in your Jenkinsfile, ask yourself if you are doing the right thing.


Video Answer


1 Answers

This should work

pipeline {      stages {         stage ('Main Stage') {             steps {                 script {                     if (true) {                         stage ('Stage 1') {                             sh 'echo Stage 1'                         }                     }                     if (false) {                         stage ('Stage 2') {                             sh 'echo Stage 2'                         }                     }                 }             }         }     } } 
like image 165
Reuben Abela Avatar answered Oct 14 '22 15:10

Reuben Abela