Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins - How to get and use upstream info in downstream

Executing upstream job called "A". On success of A executing test cases which is downstream project "B". But while sending mail from B we have to incorporate upstream project details (upstream project name, build no) in mail. So we can easily map / corelate the test run with respective upstream job.

In downstream project dashboard below details are displaying.

Started by upstream project Dev_RM_3.0_CI_Test build number 10
originally caused by:

I checked in https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project. but couldnt find anything to inherit in downstream.

Created sample job with below details to display the current job details.

echo $BUILD_NUMBER
echo $JOB_NAME
echo $BUILD_ID

But the output is

Building on master in workspace /var/lib/jenkins/workspace/env
[env] $ /bin/sh -xe /tmp/hudson970280339057643719.sh
+ echo 1
1
+ echo env
env
+ echo 1
1
Finished: SUCCESS
  1. Any help to inherit upstream details in downstream job?
  2. How to get current job details?
like image 768
divakar.scm Avatar asked Aug 29 '16 13:08

divakar.scm


2 Answers

The message that you refer to your question "Started by upstream project "Chained/1-First" build number 34" for example, is available in the jenkins Cause.

Jenkins keeps the upstream build info in it's cause object. If your are using build DSL or Pipelines you may get it in groovy. Alternatively you can curl the job url and use jq to get the Cause

For example curl http://localhost:8080/job/Chained/job/2-Second/17/api/json

"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [{
  "_class": "hudson.model.CauseAction",
  "causes": [{
    "_class": "hudson.model.Cause$UpstreamCause",
    "shortDescription": "Started by upstream project \"Chained/1-First\" build number 34",
    "upstreamBuild": 34,
    "upstreamProject": "Chained/1-First",
    "upstreamUrl": "job/Chained/job/1-First/"
  }]
}

Or from the pipeline for example:

node() {
    stage('downstream') {
        def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
        echo upstream?.shortDescription
    }
}

You can get a bunch of information out of Cause, pending all the script approvals or a global shared step. You will get a null if a different cause triggers this build, eg commit, or user.

like image 86
razboy Avatar answered Sep 20 '22 05:09

razboy


You can pass in the upstream variables via build parameters to the downstream job and then you can access them (in the downstream job) using things such as ${MyParameter1} and ${MyParameter2}.

You would need to:

  1. Add build parameters to the downstream job. For example, a string parameter named "ParentJobName".
  2. Add a post build "Trigger downstream parameterized builds on other projects" to the upstream job.
  3. Add something like "Current Build parameters" or "Predefined parameters" to the #2 and pass in whatever you need. For example: ParentJobName=${JOB_NAME}
  4. Access the parameters as you would other build variables. e.g. ${ParentJobName}

You should be able to pass in the basic stuff that way. Anything more complicated than that and you will probably be better off using a plugin like Copy Artifacts Plugin to copy files or using the Jenkins API in a system groovy step to get/modify the upstream build, etc.

like image 23
Daniel Omoto Avatar answered Sep 22 '22 05:09

Daniel Omoto