import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
stage('regular stage') {
echo 'some stage'
}
stage('skippable stage') { // this { defines a Groovy closure
// get it from params, or based on previous pipeline shenanigans
boolean shouldSkip = true
if (shouldSkip) { // this { is a part of "regular {} syntax"
echo 'skipping stage because reasons'
// STAGE_NAME is a magical variable that gets injected into closure context by stage() function
Utils.markStageSkippedForConditional(STAGE_NAME)
// this exits the current closure
return
}
echo 'continuing stage since no reason'
}
stage('another stage') {
echo 'same old stage'
}
Works with Blue Ocean:
Works with ol’ silly stage table:
If you are interested how this works under the hood - read up on Closures in Groovy (the particulars of context injection are described under “Delegates” section)

