如何在 jenkinsfile/script 中知道当前构建是重播? [英] How to know inside jenkinsfile / script that current build is a replay?

查看:11
本文介绍了如何在 jenkinsfile/script 中知道当前构建是重播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如主题 - 有什么方法可以验证当前构建是否是使用重播"按钮的效果?

As in subject - is there any way to verify if current build is effect of using 'Replay' button?

推荐答案

我使用 currentBuild 中的 rawBuild 实例找到了以下解决方案.由于我们无法获取原因的类别,因此我们只是验证其字符串值.

I found the following solution using the rawBuild instance from currentBuild. Since we can't get the class of the causes, we just verify its string value.

def replayClassName = "org.jenkinsci.plugins.workflow.cps.replay.ReplayCause​"
def isReplay = currentBuild.rawBuild.getCauses().any{ cause -> cause.toString().contains(replayClassName) }

此解决方案适用于带有 Blue-Ocean 的 Jenkins.如何获得此答案的参考是 Jenkins 声明性管道:find输出触发工作

This solution works for Jenkins with Blue-Ocean. References to how to get to this answer is Jenkins declarative pipeline: find out triggering job

使用这个步骤条件就像一个魅力!

Using this a step condition works like a charm!

您可以定义一个共享库,例如 jenkins.groovy

You can define a shared library like jenkins.groovy

def isBuildAReplay() {
  // https://stackoverflow.com/questions/51555910/how-to-know-inside-jenkinsfile-script-that-current-build-is-an-replay/52302879#52302879
  def replyClassName = "org.jenkinsci.plugins.workflow.cps.replay.ReplayCause"
  currentBuild.rawBuild.getCauses().any{ cause -> cause.toString().contains(replyClassName) }
}

您可以在 Jenkins 管道中重复使用它

You can reuse it in a Jenkins pipeline

stage('Conditional Stage') {
  when {
    expression { jenkins.isBuildAReplay() }
  }
  steps {
    ...
    ...
  }
}

这篇关于如何在 jenkinsfile/script 中知道当前构建是重播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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