How do I get the build number from where I executed a rebuild?

北慕城南 提交于 2019-12-10 17:37:31

问题


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

  1. The currentBuild variable has a field rawBuild that return a hudson.model.Run object
  2. Call rawBuildObject#getCauses() or rawBuildObject#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

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