What do `beforeAgent`, `beforeInput` and `beforeOptions` do inside `when`?
AdvancedConceptDeclarative pipeline syntax
Answer
By default a stage's when is evaluated after entering its agent, after its input and after its options are applied. That wastes a pod on a skipped stage, or asks for approval on a branch that will skip anyway.
stage('Deploy prod') {
agent { kubernetes { yamlFile 'ci/deploy-pod.yaml' } }
when { beforeAgent true; beforeInput true; branch 'main' }
input { message 'Deploy to prod?'; submitter 'release-managers' }
steps { sh './deploy.sh prod' }
}
beforeAgent true skips pod creation; beforeInput true skips the prompt on non-main branches.
More on Declarative pipeline syntax
What is the minimum valid declarative pipeline?BeginnerWhat agent types exist in declarative pipelines?IntermediateWhen do you use `agent none` at the top level?IntermediateHow do nested sequential stages work and why are they useful?IntermediateList the `post` conditions and when each fires.IntermediateWhat are the most useful `when` conditions?Intermediate