Interview Prep/Jenkins/Declarative pipeline syntax

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.

All Jenkins questions Roadmap: Jenkins

More on Declarative pipeline syntax