Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: pass git commit hash to a downstream job

Tags:

git

jenkins

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)

like image 256
Elifarley Avatar asked May 25 '16 13:05

Elifarley


People also ask

How do I deploy a specific commit in Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.

What is hash In git commit?

Every commit to a repository has a unique identifier called a hash (since it is generated by running the changes through a pseudo-random number generator called a hash function).

Can git commit Jenkins?

Your GitHub repository is integrated with your Jenkins project. With this Jenkins GitHub integration, you can now use any file found in the GitHub repository and trigger the Jenkins job to run with every code commit.


2 Answers

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, e.g. "origin/master" or "origin/foo"

etc.

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

like image 56
Dvir669 Avatar answered Oct 26 '22 00:10

Dvir669


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.

like image 45
ElpieKay Avatar answered Oct 26 '22 00:10

ElpieKay