如何在 Jenkins 流水线或多分支流水线中获取 SCM URL? [英] How do I get the SCM URL inside a Jenkins Pipeline or Multibranch Pipeline?

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

问题描述

我正在尝试让预构建合并在多分支管道中工作,并且我希望避免在我的管道脚本中对 git url 进行硬编码.

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.

似乎 scm 步骤必须以某种方式存储 url,但我不知道如何访问它.

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

推荐答案

你是对的,scm 对象确实有你需要的信息.

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

当在 Pipeline 项目(或 Multibranch Pipeline 项目)中使用 git 作为源代码控制时,scm 全局变量将是 GitSCM.这意味着 `scm.getUserRemoteConfigs()' 将返回 UserRemoteConfig 实例.这些实例具有 git 远程的名称、url 和 refspec.您可以遍历该列表以找到匹配的遥控器,或者如果您确定只有一个 url,则只使用第一个.

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()

注意事项

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

    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 - 在使用源自 GitHub 的多分支管道进行测试时,getUserRemoteConfigs 返回了两个 UserRemoteConfig 实例,一个用于常规分支,另一个用于另一个用于拉取请求.这些有相同的网址,所以没什么大不了的,但要记住一些事情.例如,在使用基于 HTTPS 的连接的项目中:

      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天全站免登陆