Interview Prep/Jenkins/Pipeline design & Jenkinsfile decisions

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 →

  1. Make the cron conditional:
    triggers { cron(env.BRANCH_NAME == 'main' ? 'H 2 * * *' : '') }
    
  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.
  3. 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.

All Jenkins questions Roadmap: Jenkins

More on Pipeline design & Jenkinsfile decisions