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

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

问题描述

有没有一种方法可以将代码从本地存储库( )中导入到 Jenkinsfile 加载步骤)?



为什么?



构建 Jenkinsfile 得到一种庞大且不可维护的特性。

现在构建作业是代码,具有相同的方法为其他代码。
也就是说,我想将它分成更小的(更易维护的)单元和单元测试它们。

我试过了




  • 共享库:允许将我们的Jenkins管道逻辑分割成单独模块中的较小文件,甚至单元测试它。

    但是,它们需要位于不同的存储库中(如果不存在在GitHub上)必须配置到Jenkins中。

  • load 步骤:允许加载groovy 脚本
    但是,这些文件必须是脚本而不是完整的常规类,这使得很难拥有多个相互依赖的文件或类。例如,继承是不可能的。

    另外,当执行重放 Jenkins的工作,这使他们很难开发和调试。 / h1>


    • 是否有一种方法(或解决方法)在与 Jeninksfile 并将该库导入到 Jenkinsfile

    • 或者还有另一种方法我还没有尝试过呢?



    示例性目录结构



    共享库我想在单个存储库中拥有以下 / em>。

     (root)
    + - someModule
    | + - ...
    + - jenkins#Jenkins在单独模块中使用的类/脚本
    | + - src#Groovy源文件
    | + - 组织
    | + - foo
    | + - Bar.groovy#for org.foo.Bar类
    | + - 测试#Groovy测试文件
    | + - 组织
    | + - foo
    | + - BarTest.groovy#测试org.foo.Bar类
    | + - pom.xml或build.groovy#为本地库构建
    + - Jenkinsfile#构建someModule,使用来自jenkins模块的类
    解决方案

     库标识符:'shared-library @ version',检索器:legacySCM(scm)

    目前采用的方法 PR 37 将无法正常使用构建代理,无论如何只适用于使用 library 步骤的脚本,而不适用于 @Library 注释。



    加载步骤 do加载的文件出现在重放中。但是,您的脚本无法静态引用此类文件中定义的类型。换句话说,你可以模拟库 vars / *。groovy 但不是 src / ** / *。groovy - 与当前PR 37相同的限制。

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

    Why?

    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.

    What I tried

    • 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.

    My Questions

    • Is there a way (or workaround) to create a shared library in the same repository as the Jeninksfile and import this library into the Jenkinsfile?
    • Or is there even another way I haven't tried, yet?

    Exemplary directory structure

    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
    

    解决方案

    Workaround:

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

    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.

    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管道共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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