git 如何在 Jenkins Pipeline 或 Multibranch Pipeline 中获取 SCM URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38254968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the SCM URL inside a Jenkins Pipeline or Multibranch Pipeline?
提问by Isaac Stefanek
I am trying to get a prebuild merge to work inside a multibranch pipeline and I would like to avoid having to hardcode the git url in my pipeline script.
我正在尝试进行预构建合并以在多分支管道中工作,并且我希望避免在我的管道脚本中硬编码 git url。
It seems like scm step must store the url somehow, but I cannot figure out how to access it.
似乎 scm step 必须以某种方式存储 url,但我不知道如何访问它。
回答by BitwiseMan
You are correct, the scm
object does have the information you need.
你是对的,scm
对象确实有你需要的信息。
When using git as the source control in a Pipeline project (or Multibranch Pipeline project), the scm
global variable will be an instance of GitSCM. That means that `scm.getUserRemoteConfigs()' will return a list of UserRemoteConfiginstances. Those instances have the git remote's name, url, and refspec. You can iterate over that list to find a matching remote, or just take the first one if your sure you only have one url.
在 Pipeline 项目(或 Multibranch Pipeline 项目)中使用 git 作为源代码控制时,scm
全局变量将是GitSCM 的一个实例。这意味着 `scm.getUserRemoteConfigs()' 将返回一个UserRemoteConfig实例列表。这些实例具有 git remote 的名称、url 和 refspec。您可以遍历该列表以查找匹配的遥控器,或者如果您确定只有一个 url,则只使用第一个。
def scmUrl = scm.getUserRemoteConfigs()[0].getUrl()
NOTES
笔记
RejectedAccessException- The
getUserRemoteConfigs
andgetUrl
methods will both throworg.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException
until you manually approve them, under "Manage Jenkins -> In-process Script Approval". The only way I've found to do this is to try running the script, have it throw an access exception, approve the one method that caused the exception, and repeat for each method until no more access exceptions are thrown. Happily the setting is server-wide, so you only have to do this once per jenkins controller, not for each pipeline job.GitHub- While testing with a GitHub-sourced multibranch pipeline,
getUserRemoteConfigs
returned two UserRemoteConfig instances, one for regular branches and another for pull requests. These had the same url so no big deal, but something to keep in mind. For example, in a project using an HTTPS-based connection:echo scm.getUserRemoteConfigs() "[ +refs/heads/*:refs/remotes/origin/* => https://github.com/bitwiseman/project.git (origin), +refs/pull/*/head:refs/remotes/origin/pr/* => https://github.com/bitwiseman/project.git (origin) ]"
RejectedAccessException-
getUserRemoteConfigs
和getUrl
方法都将抛出,org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException
直到您手动批准它们,在“管理 Jenkins -> 进程内脚本批准”下。我发现这样做的唯一方法是尝试运行脚本,让它抛出访问异常,批准导致异常的一个方法,并对每个方法重复,直到不再抛出访问异常为止。令人高兴的是,该设置是服务器范围的,因此您只需为每个 jenkins 控制器执行一次此操作,而不是为每个管道作业执行此操作。GitHub- 使用源自GitHub 的多分支管道进行测试时,
getUserRemoteConfigs
返回了两个 UserRemoteConfig 实例,一个用于常规分支,另一个用于拉取请求。这些有相同的网址,所以没什么大不了的,但要记住一些事情。例如,在使用基于 HTTPS 的连接的项目中:echo scm.getUserRemoteConfigs() "[ +refs/heads/*:refs/remotes/origin/* => https://github.com/bitwiseman/project.git (origin), +refs/pull/*/head:refs/remotes/origin/pr/* => https://github.com/bitwiseman/project.git (origin) ]"
回答by akhy
Inspired by a comment in answer by @BitwiseMan, I have found a (hacky) way to get the URL without RejectedAccessException:
受到@BitwiseMan 的评论的启发,我找到了一种(hacky)方法来获取没有 RejectedAccessException 的 URL:
checkout scm
def url = sh(returnStdout: true, script: 'git config remote.origin.url').trim()
Please note that it must be done after checkout scm
. Basically, you must be in a checked out git repository (i.e. has .git/config
file in it)
请注意,必须在 之后完成checkout scm
。基本上,您必须在已检出的 git 存储库中(即其中有.git/config
文件)