15

I'm using Jenkins Scripted Pipeline that uses Groovy style scripting, and created a Jenkinsfile to describe the pipeline. I need to create the workspace with the folder name same as git repo name, and then checkout the code in the workspace folder. My question is, before doing the checkout scm, is there a way to know the git repo name or the git repo url?

StephenKing
  • 36,187
  • 11
  • 83
  • 112
srini
  • 1,110
  • 3
  • 11
  • 20
  • Just a quick clarification, by Jenkins scripted pipeline you mean the Jenkinsfile coded in Jenkins pipeline definition or Jenkinsfile from the SCM? – Manish Joshi Aug 15 '17 at 07:11
  • There's the `scm` variable, which might have some info on that. That you need to create the workspace folder feels a bit odd to me. – StephenKing Aug 15 '17 at 09:29
  • @manish-joshi - I have the Jenkinsfile saved in the repo. – srini Aug 15 '17 at 17:33
  • @StephenKing, I need the workspace folder in a specific order, because Go (golang) has specific convention that I need to follow inorder for the dependencies to work properly. – srini Aug 15 '17 at 17:34
  • Possible duplicate of [How to get scm url in build script for jenkins multibranch workflow project](https://stackoverflow.com/questions/38254968/how-to-get-scm-url-in-build-script-for-jenkins-multibranch-workflow-project) – BitwiseMan Nov 16 '17 at 20:40

3 Answers3

21
String determineRepoName() {
    return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\\.")[0]
}

This relatively ugly code is what I use to get the repoName. The key is that the URL of the repo is stored in:

scm.getUserRemoteConfigs()[0].getUrl()

from there you need to do some string ops to get what you want.


Update:

String determineRepoName() {
    return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/').last().split("\\.")[0]
}

This works also for repositories with a deeper hierarchy (https://domain/project/subproject/repo or ssh git repo which does not contain the two // at the start.

Community
  • 1
  • 1
herm
  • 14,613
  • 7
  • 41
  • 62
  • 1
    i got the this error `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.plugins.git.GitSCM getUserRemoteConfigs` – srini Aug 15 '17 at 17:41
  • 1
    i used the hint from https://stackoverflow.com/a/38255364/2018909 to approve the script exception and was able to get the repo name. thanks @herm – srini Aug 15 '17 at 18:10
  • 2
    I just did `.tokenize('/.')[-2]` which seems to get the same result more concisely. – Ben Lee Apr 11 '18 at 19:20
  • 1
    you might want to use `String determineRepoName() { return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/').last().split("\\.git")[0] }` in case your repository name includes dots – Aurimas Stands with Ukraine Apr 10 '19 at 10:49
1

Maybe a silly answer, but isn't it possible using the environment Jenkins environment variable env.BITBUCKET_REPOSITORY?

Brini
  • 77
  • 5
0

If your Jenkins server uses Git Plugin, you could use GIT_URL env variable to shorten your code. It support most Git Server (e.g. Bitbucket, Github, GitLab, Gitea, Tuleap)

GIT_URL.tokenize('/.')[-2]

PS: Crediting Ben Lee for his suggestion on the tokenizer tips below.

Yeo
  • 11,416
  • 6
  • 63
  • 90