Jenkins declarative pipeline: Execute stage when file has changed or a new branch was created

六眼飞鱼酱① 提交于 2019-12-10 13:09:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!