Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins declarative pipeline: find out triggering job

We have a Jenkins job that uses a declarative pipeline.

This job can be triggered by different other builds.

In the declarative pipeline how can I find out which build has triggered the pipeline?

like image 959
Harold L. Brown Avatar asked Oct 24 '17 10:10

Harold L. Brown


2 Answers

Code sample below

pipeline {
    agent any
    stages {
        stage('find upstream job') {
            steps {
                script {
                    def causes = currentBuild.rawBuild.getCauses()
                    for(cause in causes) {
                        if (cause.class.toString().contains("UpstreamCause")) {
                            println "This job was caused by job " + cause.upstreamProject
                        } else {
                            println "Root cause : " + cause.toString()
                        }
                    }
                }      
            }
        }
    }
}

You can check the job's REST API to get extra information like below

{
  "_class" : "org.jenkinsci.plugins.workflow.job.WorkflowRun",
  "actions" : [
    {
      "_class" : "hudson.model.ParametersAction",
      "parameters" : [

      ]
    },
    {
      "_class" : "hudson.model.CauseAction",
      "causes" : [
        {
          "_class" : "hudson.model.Cause$UpstreamCause",
          "shortDescription" : "Started by upstream project \"larrycai-sto-46908390\" build number 7",
          "upstreamBuild" : 7,
          "upstreamProject" : "larrycai-sto-46908390",
          "upstreamUrl" : "job/larrycai-sto-46908390/"
        }
      ]
    },

Reference:

  • https://jenkins.io/doc/pipeline/examples/#get-build-cause
  • Get Jenkins upstream jobs
like image 69
Larry Cai Avatar answered Sep 21 '22 18:09

Larry Cai


I realize that this is a couple years old, but the previous response required some additional security setup in my Jenkins instance. After a bit of research, I found that there was a new feature request completed in 11/2018 that addresses this need and exposes build causes in currentBuild. Here is a little lib I wrote that returns the cause with the string "JOB/" prepended if the build was triggered by another build:

def call(body) {
    if (body == null) {body = {DEBUG = false}}
    def myParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = myParams
    body()

    def causes = currentBuild.getBuildCauses()
    if (myParams.DEBUG) { 
        echo "causes count: " + causes.size().toString()
        echo "causes text : " + causes.toString()
    }
    for(cause in causes) {
        // echo cause
        if (cause._class.toString().contains("UpstreamCause")) {
            return "JOB/" + cause.upstreamProject
        } else {
            return cause.toString()
        }
    }
}

To use this, I place it in a library in a file named "buildCause.groovy". Then I reference the library at the top of my Jenkinsfile:

library identifier: 'lib@master', retriever: modernSCM(
        [$class: 'GitSCMSource', remote: '<LIBRARY_REPO_URL>',
         credentialsId: '<LIBRARY_REPO_CRED_ID', includes: '*'])

Then I can call it as needed within my pipeline:

def cause=buildCause()
echo cause
if (!cause.contains('JOB/')) {
    echo "started by user"
} else {
    echo "triggered by job"
}
like image 29
Daniel Fitzpatrick Avatar answered Sep 18 '22 18:09

Daniel Fitzpatrick