简体   繁体   English

如何在Jenkins管道或Multibranch管道中获取SCM URL?

[英]How do I get the SCM URL inside a Jenkins Pipeline or Multibranch Pipeline?

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. 我试图让prebuild合并在multibranch管道中工作,我想避免在我的管道脚本中硬编码git url。

It seems like scm step must store the url somehow, but I cannot figure out how to access it. 似乎scm step必须以某种方式存储url,但我无法弄清楚如何访问它。

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 . 当使用git作为Pipeline项目(或Multibranch Pipeline项目)中的源控件时, scm全局变量将是GitSCM的一个实例。 That means that `scm.getUserRemoteConfigs()' will return a list of UserRemoteConfig instances. 这意味着`scm.getUserRemoteConfigs()'将返回UserRemoteConfig实例列表。 Those instances have the git remote's name, url, and refspec. 这些实例具有git remote的名称,url和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. 您可以遍历该列表以查找匹配的遥控器,或者如果您确定只有一个网址,则可以选择第一个。

def scmUrl = scm.getUserRemoteConfigs()[0].getUrl()

NOTES 笔记

  • RejectedAccessException - The getUserRemoteConfigs and getUrl methods will both throw org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException until you manually approve them, under "Manage Jenkins -> In-process Script Approval". RejectedAccessException - getUserRemoteConfigsgetUrl方法都将抛出org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException直到您手动批准它们,在“管理Jenkins - >进程内脚本批准”下。 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. 令人高兴的是,该设置是服务器范围的,因此每个jenkins控制器只需执行一次,而不是每个管道作业。

  • GitHub - While testing with a GitHub -sourced multibranch pipeline, getUserRemoteConfigs returned two UserRemoteConfig instances, one for regular branches and another for pull requests. GitHub - 在使用GitHub -sourced multibranch管道进行测试时, getUserRemoteConfigs返回了两个UserRemoteConfig实例,一个用于常规分支,另一个用于拉取请求。 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: 例如,在使用基于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) ]" 

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 . 请注意,必须在checkout scm Basically, you must be in a checked out git repository (ie has .git/config file in it) 基本上,你必须在一个签出的git仓库(即其中有.git/config文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM