问题描述
我正在使用使用 Groovy 样式脚本的 Jenkins Scripted Pipeline,并创建了一个 Jenkinsfile 来描述该管道.我需要创建文件夹名称与 git repo 名称相同的工作区,然后签出工作区文件夹中的代码.我的问题是,在执行 checkout scm
之前,有没有办法知道 git repo 名称或 git repo url?
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?
推荐答案
String determineRepoName() {
return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\.")[0]
}
这个相对难看的代码是我用来获取 repoName 的.关键是repo的URL存放在:
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.
更新:
String determineRepoName() {
return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/').last().split("\.")[0]
}
这也适用于具有更深层次结构的存储库(https://domain/project/subproject/repo 或 ssh git repo 不包含开头的两个//.
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.
这篇关于如何在 Jenkins 管道中获取 repo 名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!