Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline if else not working

Tags:

jenkins

groovy

I am creating a sample jenkins pipeline, here is the code.

pipeline {     agent any       stages {             stage('test') {              steps {                  sh 'echo hello'             }                     }         stage('test1') {              steps {                  sh 'echo $TEST'             }                     }         stage('test3') {             if (env.BRANCH_NAME == 'master') {                 echo 'I only execute on the master branch'             } else {                 echo 'I execute elsewhere'             }                                 }             } } 

this pipeline fails with following error logs

Started by user admin org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 15: Not a valid stage section definition: "if (env.BRANCH_NAME == 'master') {                 echo 'I only execute on the master branch'             } else {                 echo 'I execute elsewhere'             }". Some extra configuration is required. @ line 15, column 9.            stage('test3') {            ^  WorkflowScript: 15: Nothing to execute within stage "test3" @ line 15, column 9.            stage('test3') {            ^ 

But when i execute the following example from this url, it executes successfully and print the else part.

node {     stage('Example') {         if (env.BRANCH_NAME == 'master') {             echo 'I only execute on the master branch'         } else {             echo 'I execute elsewhere'         }     } } 

The only difference i can see is that in the working example there is no stages but in my case it has.

What is wrong here, can anyone please suggest?

like image 724
Shahzeb Khan Avatar asked Apr 24 '17 12:04

Shahzeb Khan


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!

When condition in Jenkins pipeline?

Jenkins “when” Directive: Execution of the pipeline stages can be controlled with conditions. These conditions must be defined in the when block within each stage. Jenkins supports a set of significant conditions that can be defined to limit stage execution. Each when block must contain at least one condition.

How do you skip a stage in Jenkins pipeline?

You can skip stages in declarative pipelines using when , so the following should work. stages { stage('Deploy') { when { equals expected: true, actual: Deploy } steps { // ... } } } If it should be totally invisible in the review pipeline, then use scripted pipelines and wrap the stage with an if statement.

How do you set parameters in Jenkins 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.


1 Answers

your first try is using declarative pipelines, and the second working one is using scripted pipelines. you need to enclose steps in a steps declaration, and you can't use if as a top-level step in declarative, so you need to wrap it in a script step. here's a working declarative version:

pipeline {     agent any      stages {         stage('test') {             steps {                 sh 'echo hello'             }         }         stage('test1') {             steps {                 sh 'echo $TEST'             }         }         stage('test3') {             steps {                 script {                     if (env.BRANCH_NAME == 'master') {                         echo 'I only execute on the master branch'                     } else {                         echo 'I execute elsewhere'                     }                 }             }         }     } } 

you can simplify this and potentially avoid the if statement (as long as you don't need the else) by using "when". See "when directive" at https://jenkins.io/doc/book/pipeline/syntax/. you can also validate jenkinsfiles using the jenkins rest api. it's super sweet. have fun with declarative pipelines in jenkins!

like image 89
burnettk Avatar answered Oct 02 '22 14:10

burnettk