gpt4 book ai didi

jenkins - 在多个类中扩展 Jenkins 工作 DSL 代码

转载 作者:行者123 更新时间:2023-12-04 02:43:30 26 4
gpt4 key购买 nike

我已经阅读了很多关于 Jenkins 工作 DSL 插件的教程,但我无法弄清楚如何创建一个工作工厂类,而该类又使用另一个生成工作的类。

我已经有很多工作 Groovy 脚本可以直接调用作业工厂并且可以正常工作并且我的作业已正确生成。我需要的是另一个类,它反过来使用以前的作业工厂生成大量作业。

我花了几个小时试图调试这个问题。我很确定它与闭包和实例化工厂对象有关。

package company.factory

import javaposse.jobdsl.dsl.DslFactory
import javaposse.jobdsl.dsl.Job

/**
* Base DSL templates for all Jenkins jobs.
*
*/
class JobFactory {
private DslFactory dslFactory

JobFactory(DslFactory dslFactory) {
this.dslFactory = dslFactory
}

Job generateBaseJob() {
dslFactory.job('TEST-1') {
logRotator(365, -1, 1, -1)
}
}

def generate() {
generateBaseJob()
}
}

将其称为 Groovy 脚本有效:
import company.factory.JobFactory

JobFactory jobfactory = new JobFactory(this)

jobfactory.generate()

现在,当我尝试用另一个类扩展该类时,它失败了。
package company.flow

import company.factory.JobFactory

class DeploymentFlow {
JobFactory jobfactory = new JobFactory(this)

def generateAllJobs() {
jobfactory.generate()
}
}

将其称为 Groovy 脚本失败:
import company.flow.DeploymentFlow

new DeploymentFlow().generateAllJobs()

错误:
javaposse.jobdsl.dsl.DslScriptException: (JobFactory.groovy, line 36) No signature of method: company.flow.DeploymentFlow.job() is applicable for argument types: (java.lang.String, company.factory.JobFactory$_generateBaseJob_closure1) values: [TEST-1, company.factory.JobFactory$_generateBaseJob_closure1@60ed3159]
Possible solutions: any(), wait(), getA(), find(), grep(), dump()
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:114)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts_closure1(AbstractDslScriptLoader.groovy:61)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:87)
at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:57)

Caused by:
groovy.lang.MissingMethodException: No signature of method: company.flow.DeploymentFlow.job() is applicable for argument types: (java.lang.String, company.factory.JobFactory$_generateBaseJob_closure1) values: [TEST-1, company.factory.JobFactory$_generateBaseJob_closure1@60ed3159]
Possible solutions: any(), wait(), getA(), find(), grep(), dump()
at company.factory.JobFactory.generateBaseJob(JobFactory.groovy:36)
at company.flow.DeploymentFlow.generateAllJobs(DeploymentFlow.groovy:31)
at script.run(script:24)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:138)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:108)
... 6 more

我需要将我的工作工厂类扩展到另一个类,因为我想生成很多工作并且我想避免重复的代码。任何帮助将不胜感激。

最佳答案

在 Google Jenkins DSL 插件组的好人的帮助下,我设法弄清楚了。
工作代码如下。
工厂类:

package company.factory

import javaposse.jobdsl.dsl.DslFactory
import javaposse.jobdsl.dsl.Job

class JobFactory {
private DslFactory dslFactory

JobFactory(DslFactory dslFactory) {
this.dslFactory = dslFactory
}

Job generateBaseJob() {
dslFactory.job('TEST-1')
}
}
部署类:
package company.flow

import company.factory.JobFactory
import javaposse.jobdsl.dsl.DslFactory

class DeploymentFlow {
JobFactory JobFactory

DeploymentFlow(DslFactory dslFactory) {
JobFactory = new JobFactory(dslFactory)
}

void generateAllJobs() {
JobFactory.generateBaseJob()
}
}
在我的种子工作中调用我的部署类:
import company.flow.DeploymentFlow

DeploymentFlow deploymentFlow = new DeploymentFlow(this)

deploymentFlow.generateAllJobs()

关于jenkins - 在多个类中扩展 Jenkins 工作 DSL 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58204593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com