【问题标题】:Execute Jenkins Pipeline step only when building a tag仅在构建标签时执行 Jenkins Pipeline 步骤
【发布时间】:2018-01-21 02:50:45
【问题描述】:

我有某些构建逻辑,例如发布,我希望 Jenkins 仅在构建 Git 标签时执行。我如何使用 Jenkins 的声明式管道来实现这一点?

换句话说,我正在尝试构建与 Travis CI 的 deploy on tags 功能等效的功能:

deploy:
  [...]
  on: 
    tags: true

有一个built-in condition to check the branch,但我没有看到一个指示标签的。

【问题讨论】:

标签: jenkins jenkins-pipeline multibranch-pipeline jenkins-declarative-pipeline


【解决方案1】:

更新:Pipeline Model Definition Plugin 的 1.2.8 版开始,您现在可以使用 buldingTag()

stage('Deploy') {
  when {
    buildingTag()
  }
  steps {
    echo 'Replace this with your actual deployment steps'
  }
}

使用Multibranch Pipeline 配置时,您可以使用expression 条件以及底层Branch API Plugin 提供的TAG_NAME 环境变量。不幸的是,您无法直接检查环境变量是否在 Groovy 级别定义(API 限制),因此您必须在 shell 中进行测试:

stage('Deploy') {
  when { expression { sh([returnStdout: true, script: 'echo $TAG_NAME | tr -d \'\n\'']) } }
  steps {
    echo 'Replace this with your actual deployment steps'
  }
}

以上内容利用了 Groovy 中非空字符串的真实性。

未来的版本中可能会引入一种更简单的方法。见jenkinsci/pipeline-model-definition-plugin#240

【讨论】:

  • 有人能解释一下第二个例子中的表达式应该做什么吗?可悲的是 buildingTag() 对我不起作用:(
  • @user568021 或其他任何人 - 似乎表达式打开了一个 shell,执行脚本以回显 TAG_NAME 环境变量,删除换行符,并返回它评估为空字符串(@ 987654332@) 或非空字符串 (true),因此如果 TAG_NAME 具有非空值,则返回 true
【解决方案2】:

我遇到了类似的情况,我通过获取分支名称来处理,如果是标签,它就像refs/tags/v101.0.0-beta8468,所以你必须提取/解析它来检查它是否是一个标签,否则它只是像@这样的分支名称987654322@。例如。

if(env.gitlabBranch.contains("tags"))
    {
        isTag = true
        echo "----------------true----------------"
        branch = env.gitlabBranch.split("/")[2]
        imageTag = branch

    }
    else
    {
        branch = "origin/$env.gitlabBranch"

    }

在结帐步骤中提到分支为

 branches: [[name: "${branch}"]

如果您想从同一个项目中结帐。 根据 isTag 变量,您可以选择运行某个阶段。 喜欢:

if(isTag) {
stage('Deploy') {
   // your logic here
}

将你的 isTag 初始化为 false :)

【讨论】:

    【解决方案3】:

    声明式管道

    仅在为 Tag 构建时执行 STAGE

    对于整个阶段,@vossad01 已经提供了在when 喜欢中使用buildingTag() 的答案

    stage('Deploy') {
      when {
        buildingTag()
        beforeAgent true
      }
      steps {
        echo 'deploy something when triggered by tag
      }
    }
    

    beforeAgent true 是可选的,确保在代理上执行之前检查条件。

    仅在构建标签时执行 STEP

    如果它只是一个阶段中的一个步骤,应该以标记为条件,您可以在 script 块内使用 Groovy 语法(如在脚本化管道中使用的那样),如下所示。

    stage('myStage') {                                              
      steps {                                                       
        echo 'some steps here that should always be executed'
        script {                                                    
          if (env.TAG_NAME) {                                       
            echo "triggered by the TAG:"                                 
            echo env.TAG_NAME                                       
          } else {                                                  
            echo "triggered by branch, PR or ..."                              
          }                                                         
        }                                                           
      }                                                             
    }
    

    为标签构建的附加提示

    您可能还对basic-branch-build-strategies-plugin 感兴趣,它可以在推送标签时触发构建(默认情况下不启用)。请参阅here 了解如何使用它。您可以通过Manage Jenkins -> Manage Plugins -> Available tab 安装它,然后使用搜索栏。

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多