9

I am trying to figure out a way to get the remote git project name in a jenkins job as a env variable. I would like to use the project name as the "local subdirectory name" when jenkins clones the repo. is there a way to do this?

maybe something like ${GIT_REPO_NAME}

I know that there is ${GIT_REPO_URL} and it includes the project name, but I need to be able to use this in the local sub directory field.

if someone has a better solution for this I am all ears.

thank you!

arrowill12
  • 1,784
  • 4
  • 29
  • 54
  • Have you tried this link? http://stackoverflow.com/questions/10625259/how-to-set-environment-variables-in-jenkins – jinggoy Aug 01 '14 at 21:08
  • I just saw that while browsing, unfortunately the pre build steps runs before the pre-scm steps so I am unable to use the GIT_URL which I need. – arrowill12 Aug 01 '14 at 21:49

6 Answers6

11

Here's a simple one-liner:

env.GIT_REPO_NAME = env.GIT_URL.replaceFirst(/^.*\/([^\/]+?).git$/, '$1')

/^.*\/([^\/]+?).git$/ is a regex that creates a back-reference to just the repo name.

ScrappyDev
  • 2,307
  • 8
  • 40
  • 60
  • 2
    Very nice! This should be built in by default! – trebor Jul 19 '19 at 14:25
  • Could you please explain me the meaning of value '$1' ? or explain more the command – biolinh Aug 11 '20 at 13:22
  • 1
    $1 is the back-reference to the value found by `([^\/]+?)` in the above regex. You can read up on Regex here: https://www.regular-expressions.info. This get's just the name of the repo from the git url. – ScrappyDev Aug 12 '20 at 15:26
2

^.*?(?::\/\/.*?\/|:)(.*).git$ use this pattern for both http and ssh if you want to get full repo path {owner}/{repo_name}

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '21 at 20:03
1

Not sure if this will work for your particular use case, but I thought I would offer it up in case it does:

By default, Jenkins will clone the git repo into the root of the job's workspace. Then you can refer to the git root within any of your scripts using the built-in $WORKSPACE that Jenkins sets, and I find it quite straightforward to work with.

khampson
  • 14,700
  • 4
  • 41
  • 43
0

This is what I did:

Using DSL in a build flow I grab the GIT_URL in a variable like this:

"def projectName = build.environment.get("GIT_URL").replaceAll('https://github.com/', '').replaceAll('.git', '')"

In this case I used an extra replaceAll('/','-') as I want to keep the organisation name in the projectName.

Otherwise you can remove it with with an extra replaceAll or getting the substring with .substring(x,y) string function.

In the end you can store the projectName in a Jenkins parameter in order to propagate the value to other builds triggered by the flow.

You can set an Env variable too with projectName value, but I wouldn't use this method as you set it with the jenkins user.

Hope this helps.

Deepend
  • 4,057
  • 17
  • 60
  • 101
Emilio.NT
  • 81
  • 2
  • 4
0

Here's what I use for Github organization builds:

def projectName() {
  // Handle branch builds
  if (env.JOB_BASE_NAME == java.net.URLEncoder.encode(env.BRANCH_NAME)) {
    jobNameSplit = env.JOB_NAME.split("/")
    switch (jobNameSplit.size()) {
      case 1:
        // project name == branch name ¯\_(ツ)_/¯
        break
      case 2:
      case 3:
        return jobNameSplit[1]
        break
      default:
        throw Error("unable to get job name")
        break
    }
  }
  return env.JOB_BASE_NAME
}

It defaults to the job name, so even if you don't use the github organization plugin, but do use job names that match your git repo name, you should be all set.

pnovotnak
  • 4,341
  • 2
  • 27
  • 38
0

I use something like env.REPO_NAME = env.GIT_URL.replace('.git', '').split('/').last()

devatherock
  • 2,423
  • 1
  • 8
  • 23