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

前端 未结 1 1167
时光取名叫无心
时光取名叫无心 2021-01-05 11:18

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

1条回答
  •  醉梦人生
    2021-01-05 12:00

    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 {
        ...
        ...
      }
    }
    

    0 讨论(0)
提交回复
热议问题