d3v-sci
d3v-sci

Reputation: 173

How to access a environment variable set in a jenkins job from a different jenkins job

I have a jenkins job Job1 that sets a environment variable say CLIENT_CODE. I would like to use this variable value in a different jenkins job Job2, and i like to fetch this variable data from the last successful build of Job1.

Setting the variable is done from a shell command in Job1. And Job2 also uses shell commands to fetch this variable value.

Please let me know an approach to do this.

Upvotes: 0

Views: 931

Answers (1)

vijay v
vijay v

Reputation: 2076

  1. If Job2 is a downstream project of Job1, you can pass the environment variable as a parameter to the downstream project like below.
build job: 'path/to/downstream/project/job2', parameters: [string(name: 'PARAM1', value: "${environment-variable-to-pass-from-job1}")

OR

  1. CURL your Job1 console log to get the environment variable and use it in job2 like below.
withCredentials([usernamePassword(credentialsId: 'credentialID', usernameVariable: 'user', passwordVariable: 'password')]) {
CLIENT_CODE = sh (script: 'curl -s -u $user:$password https://job-url/job/jobName/${JOB_NUMBER}/consoleText | grep "CLIENT_CODE" | sed \'s/.*=//\'', returnStdout: true).trim()
}

OR

  1. This SO thread's 1st answer: How to set environment variable from a job and use it in next job in jenkins?

Upvotes: 1

Related Questions