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?