Interview Prep/CI/CD/Jenkins Pipelines and Agents

Only the main branch should deploy; feature branches and PRs should only build and test. How do you express this?

BeginnerScenarioJenkins Pipelines and Agents
Answer

Situation read → One Jenkinsfile, different behaviour per branch.

What I'd do →

stage('Deploy') {
  when { branch 'main' }
  steps { sh './deploy.sh prod' }
}

For PRs use when { changeRequest() }, and combine with allOf/anyOf where needed.

Why → Declarative when is evaluated before the stage and keeps the stage visibly skipped in Blue Ocean and Stage View.

Interview tip → Add that deploy to prod should also sit behind an approval, not just a branch check.

All CI/CD questions

More on Jenkins Pipelines and Agents