5

I am triggering a job (child job) in 'Server B' from a job (parent job) in 'Server A' through a python script. I have 2-3 parent jobs. So I want to know which parent job is triggered the child job. How can I know which parent job triggered child job?

Can I pass the parent job name to child job name ? Or Can I get the parent name directly from child job ? (Environment variable / using python scripts)

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
Sreevalsa E
  • 905
  • 5
  • 17
  • 39

4 Answers4

5

Every build has an environment variable JOB_NAME. You can pass this as a string parameter to your child job.

Following description is provided in the /env-vars.html:

JOB_NAME

Name of the project of this build, such as "foo" or "foo/bar". (To strip off folder paths from a Bourne shell script, try: ${JOB_NAME##*/})

OltzU
  • 666
  • 7
  • 4
2

Passing the job name as an environment variable as mentioned by OltzU, may be the way to go, but that depends on how you are triggering the child job. If you are directly triggering the child job from the parent job using a post-build step, you can use something like the Parameterized Remote Build plugin to pass the Job name along. If you are using a script in the parent job to fire off the child job, you can string the Job name on as a parameter.

If you can't pass the triggering job as a parameter, you can programmatically get the build trigger(s) using Groovy. Groovy is really the only language that fully integrates with the Jenkins api, so if you want to use another language (python), you are stuck with using the rest api or jenkins cli, which are both limited in what they can give you (e.g. neither can give you the triggering job to my knowledge).

If you want to use groovy to get the trigger job, you will need the Groovy Plugin, which you will run as build step in your child job. Here's a snippet of code to get the chain of upstream jobs that triggered your build. You may need to modify the code depending on the type of trigger that is used.

def getUpstreamProjectTriggers(causes) {
    def upstreamCauses = []
    for (cause in causes) {
        if (cause.class.toString().contains("UpstreamCause")) {         
            upstreamCauses.add(cause.getUpstreamProject())
        }
    }
    return upstreamCauses
}

getUpstreamProjectTriggers(build.getCauses())

From here, if you want to use the triggering job in, say, a python script, you would need to use groovy to set the triggering job in an environment variable. This SO thread gives more info on that, or you can skip to my answer in that thread to see how I do it.

Community
  • 1
  • 1
TheEllis
  • 1,666
  • 11
  • 18
2

In the child Jenkinsfile this Groovy code will get the name of the triggering job:

String getTriggeringProjectName() {
    if (currentBuild.upstreamBuilds) {
        return currentBuild.upstreamBuilds[0].projectName
    } else {
        return ""
    }
}

currentBuild.upstreamBuilds is a list of RunWrapper objects

Joman68
  • 2,248
  • 3
  • 34
  • 36
0

You could use an additional parameter for your child job.

SlashGordon
  • 720
  • 8
  • 11