问题
When I click "rebuild" from the page of a build jenkins rebuilds and runs a new job- a new job with a new jenkins build number.
How do I get the build number of the job where I executed the rebuild?
Im not talking about the previous build number.
Say Im on build 10. I go to build 5 and click rebuild. How do I that build number (5) from inside the pipeline like I can with env.BUILD_NUMBER
?
回答1:
I assume that you are using Groovy Pipeline and already know the Global Variable (see Global Variable Reference).
- The
currentBuild
variable has a fieldrawBuild
that return a hudson.model.Run object - Call
rawBuildObject#getCauses()
orrawBuildObject#getCauses()
and return some Cause object.
script below:
node {
stage('test advance script') {
echo "current build number: ${currentBuild.number}"
echo "previous build number: ${currentBuild.previousBuild.getNumber()}"
def causes = currentBuild.rawBuild.getCauses()
echo "causes: ${causes}"
def rebuildCause0 = currentBuild.rawBuild.getCause(com.sonyericsson.rebuild.RebuildCause)
echo "rebuildCause0: ${rebuildCause0}"
echo "rebuild up number: ${rebuildCause0.getUpstreamBuild()}"
}
}
But as we discuss in chat, the Rebuilder Plugin use CauseAction
in a wrong way. If it is fixed as this, console output should be:
current build number: 72
previous build number: 71
causes: [hudson.model.Cause$UserIdCause@679c1066, job/DMP/job/test-pipeline/63[hudson.model.Cause$UserIdCause@679c1066]]
rebuildCause0: job/DMP/job/test-pipeline/63[hudson.model.Cause$UserIdCause@679c1066]
rebuild up number: 63
Remember to
scriptApproval
when you see errors like this:
Scripts not permitted to use method hudson.model.Run getCauses. Administrators can decide whether to approve or reject this signature.
来源:https://stackoverflow.com/questions/51974682/how-do-i-get-the-build-number-from-where-i-executed-a-rebuild