如何在 Jenkins Job DSL 插件的上下文中使用共享/常见的时髦方法



使用 GroovyConsole 我有文件 main.groovy 与:

new Helpers().test("test method called")

并在同一目录中有文件 助手.groovy 与内容

def test(String str) {
    println "test method called with: " + str
}

运行结果为结果:

groovy> new Helpers().test("test method called") 
test method called with: test method called

但是,在使用DSL的Jenkins上下文中,我在file generator.groovy中有类似的代码:

new Helpers().test("test method called")

然后在 Helpers.groovy 在相同的目录中,我有:

def test(String str) {
    println("test method called on: " + str)
}

但是,当我运行时,我在日志中没有得到任何输出(来自 println(。如果我的def在同一个main.groovy文件中,它可以正常工作。

可能缺少一些基本的东西。它在 jenkins 中是编译/绿色的,所以不确定如何适应它,所以运行时会做我想做的。

从其他文件调用方法时需要导入类

在与DSL相同的级别创建一个名为utilities的目录,并在实用程序目录中创建一个名为MyUtilities.groovy的文件,其中包含以下内容:

package utilities
class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}

然后从 DSL 中添加如下内容:

import utilities.MyUtilities

最新更新