问题
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?
回答1:
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..."
}
}
}
}
来源:https://stackoverflow.com/questions/50113554/jenkins-declarative-pipeline-execute-stage-when-file-has-changed-or-a-new-branc