How to know inside jenkinsfile / script that current build is a replay?

痴心易碎 提交于 2019-12-22 06:37:42

问题


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


回答1:


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

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

Update

Using this a step condition works like a charm!

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

You can reuse it in a Jenkins pipeline

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


来源:https://stackoverflow.com/questions/51555910/how-to-know-inside-jenkinsfile-script-that-current-build-is-a-replay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!