The Jenkins documentation makes a reference to currentBuild.buildVariables for fetching variables from downstream jobs.
for a Pipeline downstream build, any variables set globally on env at the time the build ends. Child Pipeline jobs can use this to report additional information to the parent job by setting variables in env. Note that build parameters are not shown in buildVariables.
It took me a bit to catch on that set globally was the key part. if you want any environment variables, like the NODE_NAME passed back to the parent job, you need to explicitly set them in the child job.
So the two pipelines I have here make use of env.MYNODE_NAME to pass env.NODE_NAME back from the child pipeline.
[2019-07-25T22:35:17.029Z] Started by upstream project "pipeline-parent/master" build number 31
[2019-07-25T22:35:17.029Z] originally caused by:
[2019-07-25T22:35:17.029Z] Started by user Ken Rachynski
[2019-07-25T22:35:17.029Z] Replayed #30
[2019-07-25T22:35:17.029Z] Running as Ken Rachynski
[2019-07-25T22:35:17.055Z] Setting origin to git@troublemaker.local:pipeline-child
[2019-07-25T22:35:17.056Z] Fetching origin...
[2019-07-25T22:35:17.535Z] Obtained Jenkinsfile from 299b4b165d98aedadc4db5dda3800a028a531538
[2019-07-25T22:35:17.536Z] Running in Durability level: MAX_SURVIVABILITY
[2019-07-25T22:35:17.550Z] Loading library pipeline-shared@master
[2019-07-25T22:35:17.550Z] Attempting to resolve master from remote references...
[2019-07-25T22:35:17.785Z] Found match: refs/heads/master revision 7cae0ae339c8d986e38c1291f0729e2abd063929
[2019-07-25T22:35:17.792Z] using credential jenkins-docker-key
[2019-07-25T22:35:17.803Z] Fetching changes from the remote Git repository
[2019-07-25T22:35:17.804Z] Fetching without tags
[2019-07-25T22:35:18.004Z] Checking out Revision 7cae0ae339c8d986e38c1291f0729e2abd063929 (master)
[2019-07-25T22:35:18.017Z] Commit message: "Change order of slackSend Parameters"
[2019-07-25T22:35:18.734Z] [Pipeline] Start of Pipeline
[2019-07-25T22:35:19.424Z] [Pipeline] node
[2019-07-25T22:35:19.454Z] Running on docker-agent-01 in /home/jenkins/workspace/pipeline-child_master
[2019-07-25T22:35:19.459Z] [Pipeline] {
[2019-07-25T22:35:19.486Z] [Pipeline] stage
[2019-07-25T22:35:19.491Z] [Pipeline] { (Declarative: Checkout SCM)
[2019-07-25T22:35:19.514Z] [Pipeline] checkout
[2019-07-25T22:35:19.538Z] using credential jenkins-docker-key
[2019-07-25T22:35:19.558Z] Fetching changes from the remote Git repository
[2019-07-25T22:35:19.571Z] Fetching without tags
[2019-07-25T22:35:19.883Z] Checking out Revision 299b4b165d98aedadc4db5dda3800a028a531538 (master)
[2019-07-25T22:35:19.918Z] Commit message: "copy NODE_NAME"
[2019-07-25T22:35:19.945Z] [Pipeline] }
[2019-07-25T22:35:19.959Z] [Pipeline] // stage
[2019-07-25T22:35:19.981Z] [Pipeline] withEnv
[2019-07-25T22:35:19.983Z] [Pipeline] {
[2019-07-25T22:35:20.015Z] [Pipeline] withEnv
[2019-07-25T22:35:20.018Z] [Pipeline] {
[2019-07-25T22:35:20.036Z] [Pipeline] stage
[2019-07-25T22:35:20.041Z] [Pipeline] { (build)
[2019-07-25T22:35:20.061Z] [Pipeline] sh
[2019-07-25T22:35:20.489Z] + printenv
[2019-07-25T22:35:20.489Z] + sort
[2019-07-25T22:35:20.489Z] AGENT_WORKDIR=/home/jenkins/agent
[2019-07-25T22:35:20.489Z] BRANCH_NAME=master
[2019-07-25T22:35:20.489Z] BUILD_DISPLAY_NAME=#32
[2019-07-25T22:35:20.489Z] BUILD_ID=32
[2019-07-25T22:35:20.489Z] BUILD_NUMBER=32
[2019-07-25T22:35:20.489Z] BUILD_TAG=jenkins-pipeline-child-master-32
[2019-07-25T22:35:20.489Z] BUILD_URL=http://troublemaker.local/job/pipeline-child/job/master/32/
[2019-07-25T22:35:20.489Z] EXECUTOR_NUMBER=5
[2019-07-25T22:35:20.489Z] GIT_AUTHOR_EMAIL=jenkins@ice-edge.com
[2019-07-25T22:35:20.489Z] GIT_AUTHOR_NAME=Jenkins Master
[2019-07-25T22:35:20.489Z] GIT_BRANCH=master
[2019-07-25T22:35:20.489Z] GIT_COMMIT=299b4b165d98aedadc4db5dda3800a028a531538
[2019-07-25T22:35:20.489Z] GIT_COMMITTER_EMAIL=jenkins@ice-edge.com
[2019-07-25T22:35:20.489Z] GIT_COMMITTER_NAME=Jenkins Master
[2019-07-25T22:35:20.489Z] GIT_PREVIOUS_COMMIT=299b4b165d98aedadc4db5dda3800a028a531538
[2019-07-25T22:35:20.489Z] GIT_PREVIOUS_SUCCESSFUL_COMMIT=299b4b165d98aedadc4db5dda3800a028a531538
[2019-07-25T22:35:20.489Z] GIT_URL=git@troublemaker.local:pipeline-child
[2019-07-25T22:35:20.489Z] HOME=/home/jenkins
[2019-07-25T22:35:20.489Z] HOSTNAME=93ed8142db35
[2019-07-25T22:35:20.489Z] HUDSON_COOKIE=bc1d0979-ce69-4493-8c71-daeafcfaea45
[2019-07-25T22:35:20.489Z] HUDSON_HOME=/var/jenkins_home
[2019-07-25T22:35:20.489Z] HUDSON_SERVER_COOKIE=94842193e9e670b1
[2019-07-25T22:35:20.489Z] HUDSON_URL=http://troublemaker.local/
[2019-07-25T22:35:20.489Z] JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u212-b04/OpenJDK8U-
[2019-07-25T22:35:20.489Z] JAVA_HOME=/usr/local/openjdk-8
[2019-07-25T22:35:20.489Z] JAVA_URL_VERSION=8u212b04
[2019-07-25T22:35:20.489Z] JAVA_VERSION=8u212-b04
[2019-07-25T22:35:20.489Z] JENKINS_HOME=/var/jenkins_home
[2019-07-25T22:35:20.489Z] JENKINS_NODE_COOKIE=8dd3b3f3-3347-45bc-802c-e541df6267d3
[2019-07-25T22:35:20.489Z] JENKINS_SERVER_COOKIE=durable-9e05b5239e981734a114cbd5352dd19e
[2019-07-25T22:35:20.489Z] JENKINS_URL=http://troublemaker.local/
[2019-07-25T22:35:20.489Z] JOB_BASE_NAME=master
[2019-07-25T22:35:20.489Z] JOB_DISPLAY_URL=http://troublemaker.local/job/pipeline-child/job/master/display/redirect
[2019-07-25T22:35:20.489Z] JOB_NAME=pipeline-child/master
[2019-07-25T22:35:20.489Z] JOB_URL=http://troublemaker.local/job/pipeline-child/job/master/
[2019-07-25T22:35:20.489Z] LANG=C.UTF-8
[2019-07-25T22:35:20.489Z] MYBUILD=0
[2019-07-25T22:35:20.489Z] MYJOB=pipeline-child
[2019-07-25T22:35:20.489Z] MYNODE_NAME=nothing
[2019-07-25T22:35:20.489Z] NODE_LABELS=docker-agent-01
[2019-07-25T22:35:20.489Z] NODE_NAME=docker-agent-01
[2019-07-25T22:35:20.489Z] PATH=/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[2019-07-25T22:35:20.489Z] PWD=/home/jenkins/workspace/pipeline-child_master
[2019-07-25T22:35:20.489Z] RUN_CHANGES_DISPLAY_URL=http://troublemaker.local/job/pipeline-child/job/master/32/display/redirect?page=changes
[2019-07-25T22:35:20.489Z] RUN_DISPLAY_URL=http://troublemaker.local/job/pipeline-child/job/master/32/display/redirect
[2019-07-25T22:35:20.489Z] STAGE_NAME=build
[2019-07-25T22:35:20.489Z] WORKSPACE=/home/jenkins/workspace/pipeline-child_master
[2019-07-25T22:35:20.516Z] [Pipeline] script
[2019-07-25T22:35:20.520Z] [Pipeline] {
[2019-07-25T22:35:20.546Z] [Pipeline] }
[2019-07-25T22:35:20.558Z] [Pipeline] // script
[2019-07-25T22:35:20.564Z] [Pipeline] }
[2019-07-25T22:35:20.574Z] [Pipeline] // stage
[2019-07-25T22:35:20.580Z] [Pipeline] }
[2019-07-25T22:35:20.590Z] [Pipeline] // withEnv
[2019-07-25T22:35:20.598Z] [Pipeline] }
[2019-07-25T22:35:20.610Z] [Pipeline] // withEnv
[2019-07-25T22:35:20.615Z] [Pipeline] }
[2019-07-25T22:35:20.633Z] Terminated
[2019-07-25T22:35:20.652Z] [Pipeline] // node
[2019-07-25T22:35:20.658Z] [Pipeline] End of Pipeline
[2019-07-25T22:35:20.666Z] Finished: SUCCESS
f
pipeline {
agent any
environment {
MYJOB = 'pipeline-child'
MYBUILD = 0
MYNODE_NAME = 'nothing'
}
stages {
stage('build') {
steps {
sh 'printenv | sort'
script {
env.MYBUILD = env.BUILD_NUMBER
env.MYNODE_NAME = env.NODE_NAME
}
}
}
}
}
[2019-07-25T22:34:39.858Z] Started by user Ken Rachynski
[2019-07-25T22:34:39.858Z] Replayed #30
[2019-07-25T22:34:39.858Z] Running as Ken Rachynski
[2019-07-25T22:34:39.901Z] Setting origin to git@troublemaker.local:pipeline-parent
[2019-07-25T22:34:39.903Z] Fetching origin...
[2019-07-25T22:34:40.553Z] Obtained Jenkinsfile from 2fccbadac5306f0481864034397e60b118d5a1e3
[2019-07-25T22:34:40.553Z] Running in Durability level: MAX_SURVIVABILITY
[2019-07-25T22:34:40.574Z] Loading library pipeline-shared@master
[2019-07-25T22:34:40.574Z] Attempting to resolve master from remote references...
[2019-07-25T22:34:40.815Z] Found match: refs/heads/master revision 7cae0ae339c8d986e38c1291f0729e2abd063929
[2019-07-25T22:34:40.825Z] using credential jenkins-docker-key
[2019-07-25T22:34:40.834Z] Fetching changes from the remote Git repository
[2019-07-25T22:34:40.837Z] Fetching without tags
[2019-07-25T22:34:41.064Z] Checking out Revision 7cae0ae339c8d986e38c1291f0729e2abd063929 (master)
[2019-07-25T22:34:41.080Z] Commit message: "Change order of slackSend Parameters"
[2019-07-25T22:34:41.244Z] [Pipeline] Start of Pipeline
[2019-07-25T22:34:41.962Z] [Pipeline] node
[2019-07-25T22:34:42.032Z] Running on docker-agent-01 in /home/jenkins/workspace/pipeline-parent_master
[2019-07-25T22:34:42.036Z] [Pipeline] {
[2019-07-25T22:34:42.066Z] [Pipeline] stage
[2019-07-25T22:34:42.072Z] [Pipeline] { (Declarative: Checkout SCM)
[2019-07-25T22:34:42.096Z] [Pipeline] checkout
[2019-07-25T22:34:42.117Z] using credential jenkins-docker-key
[2019-07-25T22:34:42.129Z] Fetching changes from the remote Git repository
[2019-07-25T22:34:42.134Z] Fetching without tags
[2019-07-25T22:34:42.375Z] Checking out Revision 2fccbadac5306f0481864034397e60b118d5a1e3 (master)
[2019-07-25T22:34:42.404Z] Commit message: "remove childBuildId and builtOn"
[2019-07-25T22:34:42.434Z] [Pipeline] }
[2019-07-25T22:34:42.444Z] [Pipeline] // stage
[2019-07-25T22:34:42.468Z] [Pipeline] withEnv
[2019-07-25T22:34:42.471Z] [Pipeline] {
[2019-07-25T22:34:42.492Z] [Pipeline] withEnv
[2019-07-25T22:34:42.495Z] [Pipeline] {
[2019-07-25T22:34:42.529Z] [Pipeline] stage
[2019-07-25T22:34:42.544Z] [Pipeline] { (build)
[2019-07-25T22:34:42.570Z] [Pipeline] sh
[2019-07-25T22:34:42.872Z] + printenv
[2019-07-25T22:34:42.872Z] + sort
[2019-07-25T22:34:42.872Z] AGENT_WORKDIR=/home/jenkins/agent
[2019-07-25T22:34:42.872Z] BRANCH_NAME=master
[2019-07-25T22:34:42.872Z] BUILD_DISPLAY_NAME=#31
[2019-07-25T22:34:42.872Z] BUILD_ID=31
[2019-07-25T22:34:42.872Z] BUILD_NUMBER=31
[2019-07-25T22:34:42.872Z] BUILD_TAG=jenkins-pipeline-parent-master-31
[2019-07-25T22:34:42.872Z] BUILD_URL=http://troublemaker.local/job/pipeline-parent/job/master/31/
[2019-07-25T22:34:42.872Z] EXECUTOR_NUMBER=4
[2019-07-25T22:34:42.872Z] GIT_AUTHOR_EMAIL=jenkins@ice-edge.com
[2019-07-25T22:34:42.872Z] GIT_AUTHOR_NAME=Jenkins Master
[2019-07-25T22:34:42.872Z] GIT_BRANCH=master
[2019-07-25T22:34:42.872Z] GIT_COMMIT=2fccbadac5306f0481864034397e60b118d5a1e3
[2019-07-25T22:34:42.872Z] GIT_COMMITTER_EMAIL=jenkins@ice-edge.com
[2019-07-25T22:34:42.872Z] GIT_COMMITTER_NAME=Jenkins Master
[2019-07-25T22:34:42.872Z] GIT_PREVIOUS_COMMIT=2fccbadac5306f0481864034397e60b118d5a1e3
[2019-07-25T22:34:42.873Z] GIT_PREVIOUS_SUCCESSFUL_COMMIT=2fccbadac5306f0481864034397e60b118d5a1e3
[2019-07-25T22:34:42.873Z] GIT_URL=git@troublemaker.local:pipeline-parent
[2019-07-25T22:34:42.873Z] HOME=/home/jenkins
[2019-07-25T22:34:42.873Z] HOSTNAME=93ed8142db35
[2019-07-25T22:34:42.873Z] HUDSON_COOKIE=b46bc5db-84cc-4c01-865d-804560af6bbc
[2019-07-25T22:34:42.873Z] HUDSON_HOME=/var/jenkins_home
[2019-07-25T22:34:42.873Z] HUDSON_SERVER_COOKIE=94842193e9e670b1
[2019-07-25T22:34:42.873Z] HUDSON_URL=http://troublemaker.local/
[2019-07-25T22:34:42.873Z] JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u212-b04/OpenJDK8U-
[2019-07-25T22:34:42.873Z] JAVA_HOME=/usr/local/openjdk-8
[2019-07-25T22:34:42.873Z] JAVA_URL_VERSION=8u212b04
[2019-07-25T22:34:42.873Z] JAVA_VERSION=8u212-b04
[2019-07-25T22:34:42.873Z] JENKINS_HOME=/var/jenkins_home
[2019-07-25T22:34:42.873Z] JENKINS_NODE_COOKIE=8b1673f2-04bf-48e3-b219-b653abcf5575
[2019-07-25T22:34:42.873Z] JENKINS_SERVER_COOKIE=durable-9f4249ba99454826ddd487ac90b5efd6
[2019-07-25T22:34:42.873Z] JENKINS_URL=http://troublemaker.local/
[2019-07-25T22:34:42.873Z] JOB_BASE_NAME=master
[2019-07-25T22:34:42.873Z] JOB_DISPLAY_URL=http://troublemaker.local/job/pipeline-parent/job/master/display/redirect
[2019-07-25T22:34:42.873Z] JOB_NAME=pipeline-parent/master
[2019-07-25T22:34:42.873Z] JOB_URL=http://troublemaker.local/job/pipeline-parent/job/master/
[2019-07-25T22:34:42.873Z] LANG=C.UTF-8
[2019-07-25T22:34:42.873Z] MYJOB=pipeline-parent
[2019-07-25T22:34:42.873Z] NODE_LABELS=docker-agent-01
[2019-07-25T22:34:42.873Z] NODE_NAME=docker-agent-01
[2019-07-25T22:34:42.873Z] PATH=/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[2019-07-25T22:34:42.873Z] PWD=/home/jenkins/workspace/pipeline-parent_master
[2019-07-25T22:34:42.873Z] RUN_CHANGES_DISPLAY_URL=http://troublemaker.local/job/pipeline-parent/job/master/31/display/redirect?page=changes
[2019-07-25T22:34:42.873Z] RUN_DISPLAY_URL=http://troublemaker.local/job/pipeline-parent/job/master/31/display/redirect
[2019-07-25T22:34:42.873Z] STAGE_NAME=build
[2019-07-25T22:34:42.873Z] WORKSPACE=/home/jenkins/workspace/pipeline-parent_master
[2019-07-25T22:34:42.893Z] [Pipeline] script
[2019-07-25T22:34:42.902Z] [Pipeline] {
[2019-07-25T22:34:42.920Z] [Pipeline] build
[2019-07-25T22:34:42.926Z] Scheduling project: pipeline-child » master
[2019-07-25T22:35:17.051Z] Starting building: pipeline-child » master #32
[2019-07-25T22:35:20.681Z] [Pipeline] }
[2019-07-25T22:35:20.694Z] [Pipeline] // script
[2019-07-25T22:35:20.709Z] [Pipeline] echo
[2019-07-25T22:35:20.712Z] =------=
[2019-07-25T22:35:20.722Z] [Pipeline] echo
[2019-07-25T22:35:20.726Z] childJob
[2019-07-25T22:35:20.736Z] [Pipeline] echo
[2019-07-25T22:35:20.740Z] name = master
[2019-07-25T22:35:20.754Z] [Pipeline] echo
[2019-07-25T22:35:20.757Z] duration = 3636
[2019-07-25T22:35:20.763Z] [Pipeline] echo
[2019-07-25T22:35:20.767Z] number = 32
[2019-07-25T22:35:20.779Z] [Pipeline] echo
[2019-07-25T22:35:20.782Z] result = SUCCESS
[2019-07-25T22:35:20.787Z] [Pipeline] echo
[2019-07-25T22:35:20.789Z] buildVariables = [MYBUILD:32, MYNODE_NAME:docker-agent-01]
[2019-07-25T22:35:20.797Z] [Pipeline] echo
[2019-07-25T22:35:20.800Z] environment = [BRANCH_NAME:master, BUILD_DISPLAY_NAME:#32, BUILD_ID:32, BUILD_NUMBER:32, BUILD_TAG:jenkins-pipeline-child-master-32, BUILD_URL:http://troublemaker.local/job/pipeline-child/job/master/32/, CLASSPATH:, HUDSON_HOME:/var/jenkins_home, HUDSON_SERVER_COOKIE:94842193e9e670b1, HUDSON_URL:http://troublemaker.local/, JENKINS_HOME:/var/jenkins_home, JENKINS_SERVER_COOKIE:94842193e9e670b1, JENKINS_URL:http://troublemaker.local/, JOB_BASE_NAME:master, JOB_DISPLAY_URL:http://troublemaker.local/job/pipeline-child/job/master/display/redirect, JOB_NAME:pipeline-child/master, JOB_URL:http://troublemaker.local/job/pipeline-child/job/master/, library.pipeline-shared.version:master, RUN_CHANGES_DISPLAY_URL:http://troublemaker.local/job/pipeline-child/job/master/32/display/redirect?page=changes, RUN_DISPLAY_URL:http://troublemaker.local/job/pipeline-child/job/master/32/display/redirect]
[2019-07-25T22:35:20.807Z] [Pipeline] echo
[2019-07-25T22:35:20.815Z] =------=
[2019-07-25T22:35:20.821Z] [Pipeline] echo
[2019-07-25T22:35:20.824Z] currentBuild.buildVariables = [:]
[2019-07-25T22:35:20.826Z] [Pipeline] }
[2019-07-25T22:35:20.850Z] [Pipeline] // stage
[2019-07-25T22:35:20.860Z] [Pipeline] }
[2019-07-25T22:35:20.870Z] [Pipeline] // withEnv
[2019-07-25T22:35:20.878Z] [Pipeline] }
[2019-07-25T22:35:20.887Z] [Pipeline] // withEnv
[2019-07-25T22:35:20.893Z] [Pipeline] }
[2019-07-25T22:35:20.913Z] [Pipeline] // node
[2019-07-25T22:35:20.920Z] [Pipeline] End of Pipeline
[2019-07-25T22:35:20.928Z] Finished: SUCCESS
def childJob = null
pipeline {
agent any
environment {
MYJOB = 'pipeline-parent'
}
stages {
stage('build') {
steps {
sh 'printenv | sort'
script {
childJob = build job: 'pipeline-child/master', propagate: true, wait: true
}
echo '=------='
echo "childJob"
echo "name = ${childJob.projectName}"
echo "duration = ${childJob.duration}"
echo "number = ${childJob.number}"
echo "result = ${childJob.result}"
echo "buildVariables = ${childJob.buildVariables}"
echo "environment = ${childJob.rawBuild.envVars}"
echo '=------='
echo "currentBuild.buildVariables = ${currentBuild.buildVariables}"
}
}
}
}
评论前必须登录!
注册