代码之家  ›  专栏  ›  技术社区  ›  Roberto Aloi

如何在jenkins脚本控制台中包含共享库?

  •  2
  • Roberto Aloi  · 技术社区  · 7 年前

    在詹金斯档案里,如果我有 Jenkins shared library 安装在别名下 my-awesome-lib ,我可以使用以下语法将其包括在内:

    @Library('my-awesome-lib')
    import ...
    

    但我怎么能从图书馆 Jenkins script console ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   marc_s MisterSmith    7 年前

    可以从脚本控制台引用库对象,如下所示:

    // get Jenkins instance
    Jenkins jenkins = Jenkins.getInstance()
    
    // get Jenkins Global Libraries
    def globalLibraries = jenkins.getDescriptor("org.jenkinsci.plugins.workflow.libs.GlobalLibraries")
    globalLibraries.getLibraries()
    

    但是,使用共享库代码并不简单,甚至可能是不可能的。

    在继续上面的代码中,假设您这样做:

    def lib = globalLibraries[0]
    

    抓住猎犬:

    def ret = lib.getRetriever()
    

    然后需要检索源代码,但是为了调用retrieve(),需要一些脚本控制台中没有的对象:

    /**
         * Obtains library sources.
         * @param name the {@link LibraryConfiguration#getName}
         * @param version the version of the library, such as from {@link LibraryConfiguration#getDefaultVersion} or an override
         * @param target a directory in which to check out sources; should create {@code src/**}{@code /*.groovy} and/or {@code vars/*.groovy}, and optionally also {@code resources/}
         * @param run a build which will use the library
         * @param listener a way to report progress
         * @throws Exception if there is any problem (use {@link AbortException} for user errors)
         */
        public abstract void retrieve(@Nonnull String name, @Nonnull String version, @Nonnull FilePath target, @Nonnull Run<?,?> run, @Nonnull TaskListener listener) throws Exception;
    

    所以也许有一种老套的方法,但我觉得不值得。