As in subject - is there any way to verify if current build is effect of using \'Replay\' button?
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
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 {
...
...
}
}