np2807
np2807

Reputation: 1160

How to do Multiple conditional execution of stage using 'when' in declarative pipeline?

I am trying to convert Scripted pipelines into Declarative Pipeline.

Here is a Pipeline:

pipeline {
    agent any
    parameters {
        string(defaultValue: '',
               description: '',
               name : 'BRANCH_NAME')
        choice (
                choices: 'DEBUG\nRELEASE\nTEST',
                description: '',
                name : 'BUILD_TYPE')
    } 
    stages {
        stage('Release build') {
           when {
               expression {params.BRANCH_NAME == "master"}
               expression {params.BUILD_TYPE == 'RELEASE'}
            }
            steps {
                echo "Executing Release\n"
            }
        } //stage
    } //stages
} // pipeline

Intension is that all the parameter values need to be compared under when and only then I wanted executed a stage.

In scripted pipeline you can use && like in snippet below.

stage('Release build') {
    if ((responses.BRANCH_NAME == 'master') && 
       (responses.BUILD_TYPE == 'RELEASE')) {
         echo "Executing Release\n"
    }
}

How to get collective return from expression in declarative pipeline?

Upvotes: 2

Views: 5289

Answers (1)

Samit Kumar Patel
Samit Kumar Patel

Reputation: 2098

It has to be like

pipeline {
    agent any
    parameters {
        string(defaultValue: '',
               description: '',
               name : 'BRANCH_NAME')
        choice (
                choices: 'DEBUG\nRELEASE\nTEST',
                description: '',
                name : 'BUILD_TYPE')
    } 
    stages {
        stage('Release build') {
           when {
               allOf {
                    expression {params.BRANCH_NAME == "master"};
                    expression {params.BUILD_TYPE == 'RELEASE'}
               }
            }
            steps {
                echo "Executing Release\n"
            }
        } //stage
    } //stages
} // pipeline

you can find other dsl support inside when here

Upvotes: 2

Related Questions