Jenkins声明性管道:文件更改或创建新分支时执行阶段 [英] Jenkins declarative pipeline: Execute stage when file has changed or a new branch was created

查看:164
本文介绍了Jenkins声明性管道:文件更改或创建新分支时执行阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当某些文件已更改时,我才喜欢在声明性管道中构建一个阶段.这可以通过以下管道来实现:

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..."
      }
    }
  }
}

当创建新分支时,由于第一次创建分支时,变更集在Jenkins中仍然为空,因此不会建立.

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.

当某些文件更改或创建新分支时,如何定义构建stagewhen条件?

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..."
      }
    }
  }
}

这篇关于Jenkins声明性管道:文件更改或创建新分支时执行阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆