Groovy - Conditional step/stage in Jenkins pipeline Code Example

Here is the solution for "Conditional step/stage in Jenkins pipeline" in Groovy.

Groovy code for Conditional step/stage in Jenkins pipeline

stage('master-branch-stuff') {
    when {
        branch 'master'
    }
    steps {
        echo 'run this stage - ony if the branch = master branch'
    }
}


stage('feature-branch-stuff') {
    when {
        branch 'feature/*'
    }
    steps {
        echo 'run this stage - only if the branch name started with feature/'
    }
}

stage('expression-branch') {
    when {
        expression {
            return env.BRANCH_NAME != 'master';
        }
    }
    steps {
        echo 'run this stage - when branch is not equal to master'
    }
}

stage('env-specific-stuff') {
    when { 
        environment name: 'NAME', value: 'this' 
    }
    steps {
        echo 'run this stage - only if the env name and value matches'
    }
}
Code by IncludeHelp, on March 13, 2023 22:46
Reference: stackoverflow

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.