Skip to content
Latchkey

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

  • changeset needs SCM history; the very first build of a branch has no prior commit to diff against and may run everything.
  • Add beforeAgent true so 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

Run this faster and cheaper on Latchkey managed runners. Start free →