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?
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!
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.
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.
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.
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.
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
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.
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' } } } } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With