How to Set Up a Monorepo Pipeline in a Jenkins Pipeline
Jenkins builds only changed monorepo components by gating each stage with a when { changeset } condition.
Give each component its own stage gated on when { changeset 'path/**' }. Add beforeAgent true so skipped stages do not spin up an agent.
Per-component changeset gating
Each component stage runs only when its files changed; beforeAgent avoids wasting an agent on skipped stages.
Jenkinsfile
pipeline {
agent any
stages {
stage('API') {
when { changeset 'packages/api/**'; beforeAgent true }
steps { sh 'npm --prefix packages/api ci && npm --prefix packages/api test' }
}
stage('Web') {
when { changeset 'packages/web/**'; beforeAgent true }
steps { sh 'npm --prefix packages/web ci && npm --prefix packages/web run build' }
}
}
}Gotchas
changesetneeds SCM history; the very first build of a branch has no prior commit to diff against and may run everything.- Add
beforeAgent trueso a skipped stage does not allocate (and bill) an agent before evaluating the condition. - For deep dependency impact (a shared lib change affecting many packages), pair changeset with an explicit build matrix.
Related guides
How to Set Up a Monorepo Pipeline in GitHub ActionsSet up a monorepo pipeline in GitHub Actions that builds only changed packages, using a change-detection job…
How to Conditionally Run a Stage in a Jenkins PipelineConditionally run a Jenkins stage with the when directive - branch, environment, expression, and changeset co…