I like to build a stage in a declarative pipeline only when certain files have changed. This can be achieved by the following pipeline:
pipeline {
agent any
stages {
stage('checkout') {
steps {
checkout scm
}
}
stage('build & push container') {
when {
anyOf {
changeset 'Dockerfile'
}
}
steps {
echo "Building..."
}
}
}
}
This does not build when a new branch is created as the changeset is still empty in Jenkins when a branch is built for the first time.
How can I define a when
condition that builds the stage
either when a certain files changes or a new branch is created?
The following pipeline did the trick for me:
pipeline {
agent any
stages {
stage('checkout') {
steps {
checkout scm
}
}
stage('build & push container') {
when {
anyOf {
changeset 'Dockerfile'
expression {
return currentBuild.number == 1
}
}
}
steps {
echo "Building..."
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With