sam
sam

Reputation: 233

Pass environment variables from parent job to child job Jenkins pipeline

I have two pipeline jobs.

  1. Parent job: This job contains Gerrit trigger and builds on every patch-set created. After building of this job I can see the Gerrit environment variables into the build environment variable section.

  2. Child job: which runs only if the gerrit_branch is not equal to master.

I want to pass all the gerrit environment variables to this job like the parent job.

Is there any way to pass all the env variable to the child job.

Upvotes: 3

Views: 3335

Answers (1)

Sourav
Sourav

Reputation: 3402

I have taken two pipeline jobs: Pipeline1 (parent job) and pipelineB (child job)

Pipeline1: I am doing SCM checkout from github where Jenkinsfile is present and in Jenkinsfile, I have called pipelineB (child job) where I am passing the parent job environment variable (GIT_BRANCH).

Pipeline1 configuration enter image description here

Jenkinsfile of Pipeline1

pipeline {
agent any
stages
{
    stage ('Build JobB')
    {
        steps {
           sh 'env'
           build job: 'pipelineB', parameters: [string(name: 'GITHUB_BRANCH', value: "${env.GIT_BRANCH}")]
       }
     }
   } 
 }

GIT_BRANCH is environment variable here. (https://plugins.jenkins.io/git/#environment-variables)

pipelineB: I have used the option This build is parameterized to capture the value of environment value being passed by parent job i.e. Pipeline1. Then, used the variable in pipeline script.

pipelineB Configuration enter image description here enter image description here

Please try it out.

Upvotes: 1

Related Questions