在 Jenkinsfile 中,如果我有 Jenkins shared library安装在别名 my-awesome-lib
下,我可以使用以下语法包含它:
@Library('my-awesome-lib')
import ...
但是我如何从 Jenkins script console 引用该库?
最佳答案
您可以像这样从脚本控制台引用库对象:
// 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;
所以可能有一种奇怪的方法可以做到这一点,但在我看来,这不值得。
关于jenkins - 如何在 Jenkins 脚本控制台中包含共享库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53636109/