14
votes

I have two jobs(A and B) that I want to B is triggered by A with the git commit used by A after A is successfully built.

The build trigger plug-in supports trigger the downstream job with the git commit used in the upstream job.

My question is how the downstream job uses the passed in commit to check out. I didn't find which variable is passed by job A, and how to use the pass-in commit value to check out code in B via Git plug-in of Jenkins?

4

4 Answers

14
votes

Jenkins Git plug-in is an intelligent tool. No specific configuration is needed. In upstream job trigger the downstream job with the Git commit used by the upstream job, downstream will automatically checkout the commit passed in by upstream.

3
votes

I had this same question. The core problem seems to be that Jenkins doesn't carry over build-time variables from the upstream job to the downstream job by default. So the GIT_COMMIT variable will be blank, unless you actually add the repository definition in the downstream job in the Source Code Management section. And from what I can tell, it does fetch the upstream git hash from the repo, so it's not just getting latest.

This was definitely a frustrating point and took me way too long to resolve.

1
votes

It seems that now (Parameterized trigger 2.37, Jenkins 2.204), "trigger parameterized build" has the option to "Pass-through Git Commit that was built". Working even without any special configuration in the downstream job. Similar question

0
votes

I had this very same problem. You need to make sure that in the "Job Notifications" tab of the downstream project, the option "This project is parameterized" is ticked. If this is not selected, it seems that the downstream job doesn't expect parameters from upstream job, and hence it ignores the parameters.

Lets assume that upstream Job is A, and downstream job is B

In Upstream Job A:

Builds --> trigger/call builds on other projects --> Projects to build --> specify your downstream job(i.e.B)

and then Add parameters --> predefined parameters(choose whatever suits you) in "parameters" specificy the parameters you want to pass to Downstream job B. For example,

GIT_COMMIT=$GIT_COMMIT

GERRIT_BRANCH=$GERRIT_BRANCH

Note that you must put one parameter name/value per line

In Downstream Project B:

Job notifications --> This project is parameterized --> Add parameters --> string Parameters

Insert the name and the default value of the parameters. For example, for the above two parameters you may insert name and default value pairs as

Name: GIT_COMMIT

Default Value: $GIT_COMMIT

Name: GERRIT_BRANCH

Default Value: $GERRIT_BRANCH

This configuration worked perfectly for me.