简体   繁体   中英

Jenkins: pass git commit hash to a downstream job

I have 2 jobs (A and B). Job A points to a git repo at URL-A. Job B points to a different git repo, at URL-B.

Once job A finishes and is stable, it should trigger job B and pass the git commit hash that was used in job A's build.

What is the simplest way to achieve that?

(Job B corresponds to a script checked out from URL-B that has 2 parameters: a git commit hash and a JAR artifact that will be wrapped in a Docker image and pushed to Docker hub)

When you checking out from Job A using the GIT plugin you already have a $GIT_COMMIT parameter that the Plugin exports :

Environment variables

The git plugin sets several environment variables you can use in your scripts:

GIT_COMMIT - SHA of the current

GIT_BRANCH - Name of the remote repository (defaults to origin), followed by name of the branch currently being used, eg "origin/master" or "origin/foo"

etc.

Just make sure to pass this parameter to Job B explicitly (predefined parameters, parameter from file, etc).

In Job B, check This project is parameterized . Define a String Parameter , set Name to COMMIT for example. In Job A, set Post-build Actions with Trigger parameterized build on other projects . Project to build is Job B's name, with Trigger when build is as Stable . Add Parameters with Predefined parameters , in which you just put COMMIT=$commit . $commit is usually a paramter defined in Job A. You could add other predefined parameters in Job B and pass a value to all or some of them in Job A.

Easy way is to use Jenkins parameterized-trigger plugin by which you can trigger a build only on previous build is stable.

You can pass the parameters as well. Like In your case set the git commit hash to parameter and call the Job B.

Where Job B would receive that parameter and process further

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.

Using (Parameterized trigger Plugin https://plugins.jenkins.io/parameterized-trigger/ ) you set the following:

On Upstream JobA: Post-Build Actions -> Trigger parameterized build on other projects -> Project to build: JobB. Add Parameter -> Pass-through Git Commit that was build.

On Downstream JobB: Just Check Source Code Management from None to Git and set same repo as in JobA. For Branch just do nothing, you can even delete branch, but Jenkins will add master automatically. Don't worry regarding Master, this JobB will use GitCommit SHA from JobA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM