A nightly job should run only on `main` in a multibranch project, but every feature branch also fires at 2 AM. What is wrong and how do you fix it?
IntermediateScenarioPipeline design & Jenkinsfile decisions
Answer
Situation read → triggers { cron('H 2 * * *') } in the Jenkinsfile registers the trigger on every branch job that has the file.
What I'd do →
- Make the cron conditional:
triggers { cron(env.BRANCH_NAME == 'main' ? 'H 2 * * *' : '') } - Note that the trigger registers after the next build of each branch, so trigger one build per branch or wait for the next push.
- Alternatively, move nightly work to a separate pipeline job targeting
main.
Why → Triggers are job properties set by running the Jenkinsfile; an empty cron string clears them.
Interview tip → Mention that properties in a Jenkinsfile only apply after the build runs once.
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