Interview Prep/Jenkins/Pipeline design & Jenkinsfile decisions

A Jenkinsfile must work for both PR builds (no push, no deploy) and `main` builds (push and deploy). Show the structure.

BeginnerScenarioPipeline design & Jenkinsfile decisions
Answer

Situation read → One file, branch-aware stages.

What I'd do →

stage('Push image') {
  when { branch 'main' }
  steps { pushImage() }
}
stage('Deploy dev') {
  when { allOf { branch 'main'; not { changeRequest() } } }
  steps { deploy('dev') }
}
  1. All PR builds run build, test, scan.
  2. Push and deploy stages run only on main.
  3. Deploy credentials are scoped so PR jobs cannot use them even if someone edits the when.

Why → One Jenkinsfile avoids drift between PR and main pipelines.

Interview tip → Always pair when conditions with credential scoping; when alone is not a security control.

All Jenkins questions Roadmap: Jenkins

More on Pipeline design & Jenkinsfile decisions