从同一存储库加载 Jenkins Pipeline 共享库 [英] Load Jenkins Pipeline Shared Library from same repository

查看:20
本文介绍了从同一存储库加载 Jenkins Pipeline 共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR 有没有办法将代码从本地 存储库导入到 Jenkinsfile (除了 load 步骤)?

TL;DR Is there a way to import code into the Jenkinsfile from the local repository (other than the load step)?

我已经体验到,对于复杂的构建,Jenkinsfile 会变得有点笨重且不太易于维护.
既然构建工作是代码,那么拥有与其他代码相同的方法会很棒.也就是说,我想把它分成更小(更易于维护)的单元并单元测试它们.

I've experienced that for complex builds the Jenkinsfile gets kind of bulky and not very maintainable.
Now that the build job is code, it would be wonderful to have the same means as for other code. That is, I would like to divide it into smaller (more maintainable) units and unit test them.

  • 共享库:允许将我们的 Jenkins 流水线逻辑分割成更小的文件在一个单独的模块中,甚至对其进行单元测试.
    但是,它们需要位于不同的存储库中,并且(如果不在 GitHub 上)必须配置到 Jenkins 中.
  • load 步骤:允许从存储库加载 groovy scripts.
    但是,这些文件必须是脚本而不是完整"的 groovy 类,因此很难拥有多个相互依赖的文件或类.例如继承是不可能的.
    此外,在 Jenkins 作业上执行 replay 时,不会显示这些文件,这使得它们难以开发和调试.
  • shared libraries: allow for dividing our Jenkins Pipeline logic into smaller files in a separate module and even unit test it.
    However, they need to be in different repository and (if not on GitHub) must be configured into Jenkins.
  • load Step: Allow for loading groovy scripts from the repository.
    However, the files must be scripts and not "full" groovy classes, making it difficult to have multiple files or classes that depend on each other. For example inheritance is impossible.
    In addition, theses files are not displayed when doing a replay on a Jenkins job, which makes them hard to develop and debug.
  • 有没有办法(或解决方法)在与 Jenkinsfile 相同的存储库中创建共享库并将此库导入 Jenkinsfile?
  • 或者还有其他我没有尝试过的方法吗?
  • Is there a way (or workaround) to create a shared library in the same repository as the Jenkinsfile and import this library into the Jenkinsfile?
  • Or is there even another way I haven't tried, yet?

类似于为 共享库 描述的目录结构 我想在单个存储库中具有以下内容.

Similar to the directory structure described for shared libs I would like to have the following in a single repository.

(root)
+- someModule
|   +- ...
+- jenkins           # Classes/Scripts used by Jenkins in a separate module
|   +- src                       # Groovy source files
|      +- org
|          +- foo
|              +- Bar.groovy     # for org.foo.Bar class
|   +- test                      # Groovy test files
|      +- org
|          +- foo
|              +- BarTest.groovy # Test for org.foo.Bar class
|   +- pom.xml or build.groovy   # Build for local library
+- Jenkinsfile     # Build "someModule", uses classes from "jenkins" module

推荐答案

解决方法:

library identifier: 'shared-library@version', retriever: legacySCM(scm)

PR 37 目前采用的方法将无法与构建代理一起正常工作,并且无论如何只能用于使用 library 步骤的脚本,而不是 @Library 注释.

The approach currently taken in PR 37 will not work properly with build agents, and anyway will only work for scripts using the library step, not the @Library annotation.

顺便说一下,从load 步骤加载的文件 会出现在Replay 中.但是您的脚本确实不能静态引用此类文件中定义的类型.换句话说,您可以模拟库 vars/*.groovy 但不能模拟 src/**/*.groovy — 与当前 PR 37 的限制相同.

By the way files loaded from the load step do appear in Replay. But it is true that your script cannot statically refer to types defined in such files. In other words, you could simulate library vars/*.groovy but not src/**/*.groovy—the same limitation as the current PR 37.

这篇关于从同一存储库加载 Jenkins Pipeline 共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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