How to Abort Older Builds With the milestone Step in Jenkins
The milestone step aborts any earlier build that has not yet passed the same milestone when a newer build reaches it.
Place a milestone step early in the pipeline. When a newer build passes that milestone, Jenkins aborts older builds still behind it. Pair with disableConcurrentBuilds for stricter serialization.
Steps
- Add a
milestone()step at the start of the pipeline. - A newer build passing the milestone aborts older builds behind it.
- Add
options { disableConcurrentBuilds() }to fully serialize a branch.
Jenkinsfile
Jenkinsfile
pipeline {
agent any
options { disableConcurrentBuilds() }
stages {
stage('Guard') {
steps { milestone(ordinal: 1) }
}
stage('Build') {
steps { sh 'make build' }
}
}
}Gotchas
- milestone only aborts builds that started earlier and have not yet reached it.
- For deploy stages you usually want serialization, not aborting work mid-release.
Related guides
How to Limit Kept Builds With buildDiscarder in JenkinsCap how many Jenkins builds and artifacts are retained with the buildDiscarder option and logRotator, keeping…
How to Set a Timeout on a Single Stage in JenkinsBound just one Jenkins stage with the timeout step so a hung deploy or flaky test is aborted without capping…