简体   繁体   中英

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.

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在此处输入图像描述

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 ie Pipeline1. Then, used the variable in pipeline script.

pipelineB Configuration在此处输入图像描述 在此处输入图像描述

Please try it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM