Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 872
red888 Avatar asked Aug 22 '18 20:08

red888


1 Answers

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.

like image 180
tianzhipeng Avatar answered Nov 10 '22 20:11

tianzhipeng