Jenkins - How to get and use upstream info in downstream

前端 未结 5 1463
我在风中等你
我在风中等你 2020-12-25 15:15

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 pr

相关标签:
5条回答
  • 2020-12-25 15:57

    You can simply use params.variableName in your downstream job to retrieve the parameters passed from your upstream parameter job. Your downstream job need not necessarily be a parameterized job.

    0 讨论(0)
  • 2020-12-25 16:02

    Extending @razboy answer: this is good way if Cause cannot be whitelisted in sandbox. I forgot about Jenkins API and used current build console to look for string about trigger cause. You can try to fetch data from API as @razboy or get current console and grep it if you need simple stuff. Jenkins API is more flexible for more complex logic. To get API help,append /api to your build url: <jenkins_url>/job/<buildUrl>/<buildNumber>/api

       def buildUrl = env.BUILD_URL
       sh "wget $buildUrl -O currentConsole.txt"
       statusCode = sh returnStatus: true,script: 'cat currentConsole.txt | grep -q "Started by upstream project"'
       boolean startedByUpstream= statusCode==0
    
    0 讨论(0)
  • 2020-12-25 16:10

    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.

    0 讨论(0)
  • 2020-12-25 16:19

    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.

    0 讨论(0)
  • 2020-12-25 16:20

    MeowRude's answer helped me. To repcap it, in upstream job:

    build job: 'mail-test', parameters: [[$class: 'StringParameterValue', name: 'VERSION_NUMBER', value: '1.0.0.0']]
    

    And in downstream job:

    echo "${params.VERSION_NUMBER}"
    
    0 讨论(0)
提交回复
热议问题