How to get build number as a parameter for downstream job from upstream job with declarative pipeline code

Viewed 52

I have Two jobs like one is CI jod & another one is CD. I want CI build number should use on CD number.. Can you please help me with declarative pipeline script to get build number as a parameter. here CI job is calling CD job.

1 Answers

Jenkins already provides a simple means to access the number of the current build using env.BUILD_NUMBER. So if you wanted to pass the build number of CI to the downstream job CD, you could do

build([
    job       : 'CD',
    parameters: [
        string(name: 'MAIN_BUILD_NUMBER', value: "${env.BUILD_NUMBER}")
    ]
])

Then in the CD job, declare a parameter like this:

parameters {
    string(defaultValue: null, description: 'Build No', name: 'MAIN_BUILD_NUMBER')
}

You should then be able to use ${env.MAIN_BUILD_NUMBER} anywhere in your CD jobs' Jenkinsfile.

Related