2

I have a pipeline Jenkins job which is working as expected, configured with the option "GitHub hook trigger for GITScm polling" for auto build on any push to git repo.

I have hooked this Jenkins URL for multiple git repo's say repo-A, repo-B, repo-B.
Jenkins job is triggering automatically on any code pushes to these repo's.

But I would like to know which repo has triggered the Jenkins job as it is configured to multiple git repo's at Jenkins level. Any help on this highly appreciated.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
Viren Sow
  • 41
  • 2
  • 4
  • 1
    You may refer to section *Environment variables* in jenkins online documentation: https://wiki.jenkins.io/display/JENKINS/Git+Plugin – Timothy Truckle Apr 17 '18 at 19:21
  • thanks for your help Timothy... i have tried to print below environment variables in my jenkins script which returns null always. **"env.GIT_BRANCH", "env.GIT_COMMIT" and "env.GIT_URL"** – Viren Sow Apr 18 '18 at 16:47

2 Answers2

2

Following an example I found on Jenkins's bug tracker in one of the tickets (example stage), this is what works in my pipeline:

stage("checkout") {
  script {
    def git_params = checkout(...)
    println(git_params)
    env.REPO = git_params["GIT_URL"]
  }
}

git_params also included: GIT_AUTHOR_EMAIL, GIT_AUTHOR_NAME, GIT_BRANCH, GIT_COMMIT, GIT_COMMITTER_EMAIL, GIT_COMMITTER_NAME, GIT_PREVIOUS_COMMIT, GIT_PREVIOUS_SUCCESSFUL_COMMIT.

Neara
  • 3,693
  • 7
  • 29
  • 40
  • This is exactly what I shared in 2018. You added the "script" wrapper which allows declarative pipelines to use groovy code, like setting variables. Also instead of setting the result in an env variable, I would use a global field with @Field – fredericrous Mar 21 '21 at 08:43
1

You can get GIT Plugin variables as a return value of step checkout

ie:

commit = checkout scm
println commit.GIT_URL

PS: your title should be "how to know which git repository and triggered the jenkins job". Indeed for getting a branch's name I would just get the value of env.BRANCH_NAME

fredericrous
  • 2,833
  • 1
  • 25
  • 26