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

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

问题描述

与主题中一样-是否有任何方法可以验证当前版本是否是使用重播"按钮的效果?

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

推荐答案

我使用来自currentBuildrawBuild实例找到了以下解决方案.由于我们无法找到原因的类别,因此只需验证其字符串值即可.

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) }

此解决方案适用于Jenkins和Blue-Ocean.关于如何获得此答案的参考是 Jenkins声明式管道:查找触发工作

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/脚本中知道当前版本是重播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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