如何获取Jenkins管道或多分支管道内的SCM URL? [英] How do I get the SCM URL inside a Jenkins Pipeline or Multibranch Pipeline?

查看:593
本文介绍了如何获取Jenkins管道或多分支管道内的SCM URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多分支管道中获得预构建合并,并且我想避免在管道脚本中对git url进行硬编码。



它看起来像scm步骤必须以某种方式存储网址,但我无法弄清楚如何访问它。

解决方案

scm 对象确实有您需要的信息。



当使用git作为Pipeline项目中的源代码控件时多分支管道项目)中, scm 全局变量将是 GitSCM 。这意味着`scm.getUserRemoteConfigs()'将返回一个列表 UserRemoteConfig 实例。那些实例有git remote的名字,url和refspec。您可以迭代该列表以查找匹配的远程,或者只要确定只有一个url,就可以获取第一个。

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



NOTES




  • RejectedAccessException - getUserRemoteConfigs getUrl 方法都会抛出 org.jenkinsci.plugins.scriptsecurity .sandbox.RejectedAccessException 直到您手动批准它们,在管理Jenkins - >进程内脚本批准下。我发现这样做的唯一方法是尝试运行脚本,让它抛出一个访问异常,批准导致该异常的一个方法,并对每个方法重复,直到不再有访问异常抛出。令人高兴的是,该设置是服务器范围内的,因此您只需要为每个jenkins控制器执行一次该操作,而不是每个管道工作。

  • GitHub - 使用 GitHub 源代码多分支管道进行测试时, getUserRemoteConfigs 返回了两个UserRemoteConfig实例,一个用于常规分支,另一个用于拉取请求。这些URL具有相同的URL,所以没有什么大不了的,但需要牢记。例如,在使用基于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)
    ]



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.

It seems like scm step must store the url somehow, but I cannot figure out how to access it.

解决方案

You are correct, the scm object does have the information you need.

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 UserRemoteConfig instances. 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.

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". 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)
    ]"
    

这篇关于如何获取Jenkins管道或多分支管道内的SCM URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆