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') }
}
- All PR builds run build, test, scan.
- Push and deploy stages run only on
main. - 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.
More on Pipeline design & Jenkinsfile decisions
A team's PR builds pile up: every push to an open PR starts a new 25-minute build while the old one keeps running. What do you change?BeginnerA deploy pipeline waits on `input` for approval, and on Monday you find 14 pods sitting idle since Friday. Fix it.IntermediateUnit tests, lint, dependency audit and SAST run sequentially and take 22 minutes. How do you restructure the pipeline?BeginnerDevelopers want to rerun only the deploy stage when it fails due to a flaky cluster API, without rebuilding the image. What do you offer?IntermediateA pipeline must run integration tests against a shared staging database that can only handle one test run at a time across four different repos. How do you coordinate?IntermediateA library must be tested on Node 18, 20 and 22, on Linux and Windows, but Node 18 on Windows is not supported. Design the stage.Intermediate