10raw
10raw

Reputation: 574

How to pass the build number from one job to another in jenkinsfile for build job?

I have a situation where the build number of one job has to be passed to another job and the next job will use that as a parameter.

 stages {
         stage('Build Job1') {
            steps {
              script {

                   build job: "001_job" 
                   $build_001= env.BUILD_NUMBER of 001_job                  
                echo env.BUILD_NUMBER //this echos the build number of this job and not 001_job
            }
            }   
        }    
      stage('job_002') {
            steps {
              script {

                   build job: "job_002", parameters: [string(name: "${PAYLOAD_PARAM}", value: "$build_001")]
            }
            }   
        }  
   }
}  

Upvotes: 0

Views: 2098

Answers (1)

10raw
10raw

Reputation: 574

I figured out a way to do this. Need to have a global environment variable and then assign the build number via funtion like in solution below:

pipeline {
    environment {
        BUILD_NUM = ''

    }
    agent {
        label 'master'
    }
 stages {
         stage('Build Job1') {
            steps {
              script {

                  def  build job: "001_job" 
                   def build_num1 = build_job.getNumber()
                 BUILD_NUM = "${build_num1}"
                 echo BUILD_NUM  //build number of oo1/job
            }
            }   
        }    
      stage('job_002') {
            steps {
              script {

                   build job: "job_002", parameters: [string(name: "${PAYLOAD_PARAM}", value: BUILD_NUM))]
            }
            }   
        }  
   }
}  

Upvotes: 1

Related Questions