Home > other >  How to fetch Build ID of Job triggered from another job
How to fetch Build ID of Job triggered from another job

Time:09-30

I have the following situation in a Jenkinsfile of Job A:

  ...
  ... // Some execution
  ...

  call Job B

  // When Job B runs successfully
  params.some_var_used_in_Job_C = BUILD ID of Job B
  call Job C

I have to know the BUILD ID of Job B after it succeeds and I need to pass it as a params to Job C. Can anyone suggest how I can do this?

Also is it possible that I can pass some variable from Job B to Job A (so that I can send that value to Job C later) ?

CodePudding user response:

pipeline {
    agent any

    stages {
        stage('Upstream') {
            steps {
                script {
                    // upstream build if available
                    def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
                    echo upstream?.shortDescription

                    // the run of that cause holds more infos
                    def upstreamRun = upstream?.getUpstreamRun()
                    echo upstreamRun?.number.toString()
                }
            }
        }
    }
}

See the api docs for the run class.

CodePudding user response:

Should be as simple as this:

node {
    stage('Test') { // for display purposes
        def jb = build wait: true, job: 'JobB'
        println jb.fullDisplayName
        println jb.id
        //this will show everything available but needs admin privs to execute
        println jb.properties 
    }
}

If you want to pass a simple string from job B to Job A then in Job B you can set an env variable

env.someVar = "some value"

then back in job A

println jb.buildVariables.someVar
  • Related