Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the build_number of a downstream job in a declarative pipeline?

In a declarative Jenkins pipeline, I need to start job B, get its build_number, so I can call another job later that references artifacts from that specific build.

Return parameters/results from a job(triggered by pipeline) back to the same pipeline looks like it would work in the older groovy Jenkinsfiles but the new declarative syntax does not allow me to define a variable based on the "build job" step.

    stage ('Provision system testbed') {
        steps {
            build job: 'Build_Testbed', parameters: [
                string(name: 'TOPOLOGY', value: 'flat')
            ]
            **// How to we get the build_number from above?**
        }
    }

    stage ('System tests') {
        steps {
            sh """
                bundle exec rake spec:system || true
            """

            step([$class: 'JUnitResultArchiver', testResults: 'results/*.xml'])
        }
    }

    stage ('Destroy system testbed') {
        steps {
             build job: 'Destroy_Testbed', parameters: [
                string(name: 'BUILD_SELECTOR', value: '<SpecificBuildSelector plugin="[email protected]">  <buildNumber>**714**</buildNumber></SpecificBuildSelector>')
            ]
        }
    }

Of course, I'm open to completely alternative solutions, too


Solution:

    stage ('Provision system testbed') {
        steps {
            script {
                def setupResult = build job: 'Build_Testbed', parameters: [
                    string(name: 'TOPOLOGY', value: 'flat')
                ]
                def systest_build_number = setupResult.getNumber()
                // Navigate to jenkins > Manage jenkins > In-process Script Approval
                // staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods putAt java.lang.Object java.lang.String java.lang.Object
                env['setup_build_number'] = setupResult.getNumber()
            }
        }
    }

    stage ('System tests') {
        steps {
            sh """
                bundle exec rake spec:system || true
            """

            step([$class: 'JUnitResultArchiver', testResults: 'results/*.xml'])
        }
    }

    stage ('Destroy system testbed') {
        steps {
             build job: 'Destroy_Testbed', parameters: [
                string(name: 'BUILD_SELECTOR', value: '<SpecificBuildSelector plugin="[email protected]">  <buildNumber>${env.setup_build_number}</buildNumber></SpecificBuildSelector>')
            ]
        }
    }

NOTE: Requires approval of the script in my Jenkins server at Jenkins > Manage jenkins > In-process Script Approval

staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods putAt java.lang.Object java.lang.String java.lang.Object
like image 364
julianje Avatar asked Oct 18 '22 08:10

julianje


1 Answers

looks like it would work in the older groovy Jenkinsfiles

you can use the script step to enclose a block of code, and, inside this block, declarative pipelines basically act like scripted, so you can still use the technique described in the answer you referenced.

welcome to stackoverflow. i hope you enjoy yourself here.

like image 175
burnettk Avatar answered Nov 26 '22 21:11

burnettk