Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the project details which has triggered the specific job?

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)

like image 591
Sreevalsa E Avatar asked May 12 '16 09:05

Sreevalsa E


People also ask

How can you trigger a build job?

Developers can follow these three steps to implement a remote Jenkins build trigger: Create a Jenkins build job and enable the Trigger builds remotely checkbox. Provide an authentication token; This can be any text string of your choice. Invoke the Jenkins build URL to remotely trigger the build job.

What is build after other projects are built in Jenkins?

Build after other projects are built If your project depends on another project build then you should select Build after other projects are built option from the build triggers. After that, It starts watching the specified projects in the Projects to watch section.


3 Answers

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##*/})

like image 192
OltzU Avatar answered Oct 22 '22 00:10

OltzU


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.

like image 34
TheEllis Avatar answered Oct 21 '22 23:10

TheEllis


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

like image 25
Joman68 Avatar answered Oct 22 '22 00:10

Joman68