0
votes

I have Jenkins pipeline which consists of 2 stages,

I want to execute second stage only on certain condition, e.g. if git branch is not master.

When it was on bash I used to use simple logic:

if [condition] {exit 0} else {stage2 function} fi

How is it possible in Jenkins pipeline groovy?

My Jenkinsfile looks like this -

pipeline {
    agent any
    stages {
        stage ('First') {
            steps {
                echo "First"
            }

            if (env.BRANCH_NAME == 'master') {
                echo 'First stage is enought, exit 0 shoul happened here'
                currentBuild.result = 'SUCCESS'
                return
            } else {
                echo 'Second stage must be executed'
            }

        }

        stage('Second') {
            steps {
                echo "Second"
            }
        }
    }
}

..and it doesn't work: enter image description here

However, it works as expected in scripted pipeline - https://github.com/kagarlickij/jenkins-pipeline/blob/scripted/Jenkinsfile

2
Try it with the branch condition inside the steps block.Griffin

2 Answers

2
votes

After some research it turned out that there's no simple way to do it, just a few workarounds.

Workarounds could be good for super-simple pipeline, but if pipeline has dozens of stages, it doesn't worse it.

So the final decision was to switch from declarative to scripted pipeline, where it's simple and obvious: enter image description here

0
votes

exit 0 inside a stage of a declarative pipeline will only exit the current stage and not the full pipeline. That's why I would recommend the following workaround (I work with a parameter but you can use an env var too of course):

pipeline {
    agent any

    parameters {
        string(defaultValue: "master", description: 'What branch?', name: 'BRANCH')
    }

    stages {
        stage ('First') {
            when { 
                expression 
                    {  params.BRANCH == 'master' } 
            }
            steps {
                echo "Branch is master"
            }
        }

        stage('Second') {
            when { 
                expression 
                    {  params.BRANCH != 'master' } 
            }
            steps {
                echo "Branch is not master"
            }
        }
    }
}

parameter: "master":

[Pipeline] {
[Pipeline] stage
[Pipeline] { (First)
[Pipeline] echo
Branch is master
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Second)
Stage 'Second' skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

parameter "something different then master":

[Pipeline] {
[Pipeline] stage
[Pipeline] { (First)
Stage 'First' skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Second)
[Pipeline] echo
Branch is not master
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS