Interview Prep/Jenkins/Pipeline design & Jenkinsfile decisions

A stage should run only when files under `infra/` changed, but on new branches it never runs. Why, and what do you do?

AdvancedScenarioPipeline design & Jenkinsfile decisions
Answer

Situation read → when { changeset 'infra/**' } compares against changes since the previous build of that branch job. The first build of a new branch has no recorded changesets, so the condition is false.

What I'd do →

  1. Compute the diff against the target branch yourself:
    when { expression { sh(script: 'git diff --name-only origin/main...HEAD | grep -q "^infra/"', returnStatus: true) == 0 } }
    
  2. Ensure the clone fetches main (not a shallow single-branch clone).
  3. For PRs, use CHANGE_TARGET instead of hardcoding main.

Why → Diffing against the merge base is deterministic regardless of build history.

Interview tip → Knowing the first-build changeset gotcha signals real Jenkins experience.

All Jenkins questions Roadmap: Jenkins

More on Pipeline design & Jenkinsfile decisions