如何在jenkinsfile/脚本中知道当前版本是重播?

如何在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 {
    ...
    ...
  }
}