如何在 Jenkins Job DSL 中创建可扩展的基础作业?

How to create an extendable base job in Jenkins Job DSL?

我正在尝试创建一个基础工作,以减少我们工作之间的重复。我做了以下操作,但没有用:

def baseJob(Map m, Closure c = {}) {
    type = m.type ?: 'dev'
    pipelineJob("prefix-${m.name}") {
        parameters {
            stringParam('ONE', 'one', 'Description one')
        }
        c()
    }
}

baseJob(type: 'release', name: 'test') {
    parameters { // <-- Fails here
        stringParam('TWO', 'two', 'Description two')
    }
}

我收到以下错误:

ERROR: (script, line 12) No signature of method: script.parameters() is applicable for argument types: (script$_run_closure1$_closure4) values: [script$_run_closure1$_closure4@18b249b3]

以下按预期工作:

def baseJob(Map m, Closure c = {}) {
    type = m.type ?: 'dev'
    pipelineJob("prefix-${m.name}") {
        parameters {
            stringParam('ONE', 'one', 'Description one')
        }
        parameters { // <-- This is fine
            stringParam('TWO', 'two', 'Description two')
        }
        c()
    }
}

baseJob(type: 'release', name: 'test')

所以问题不在于我多次调用 parameters。问题似乎是我从闭包内部调用 parameters

我愿意相信有一种方法可以执行闭包,因此可以正确调用 parameters。但是,我怀疑在我能够弄明白之前,我必须学习更多关于 Groovy 和 Jenkins Job DSL 的知识。所以我希望有人知道该怎么做。

如果您有替代解决方案来完成可扩展的基本工作,那也是一个有效的答案。

Answer : The method parameters is not implemented inside your script. It was actually implemented inside the pipeline Closure delegate class.

此代码可以帮助您了解那里发生的事情....

class Test {
    void printMe()
    {
        println "I am in test"
    }    
}

void test(Closure cl)
{
    Test t = new Test()
    cl.delegate = t
    cl()
}

def callMe = { printMe()}
test { 
    printMe() // This will run
    callMe() // This will fail
}

你的情况: pipeline (String arg, Closure pipeLineClosure)

pipeLineClousure 已在 class X 中实现,其中可以找到 parameters 方法。如下代码所示,

class X
{
  ...
  parameters (Closure cl)
}

所以可能的实现可能是:

class Custom{
    String val1
    String val2
    String val3
}

def baseJob(Map m, List<Custom> l) {
    type = m.type ?: 'dev'
    pipelineJob("prefix-${m.name}") {
        l.each{v->
            parameters {
                stringParam(v.val1, v.val2, v.val3)
            }
        }
    }
}

List l = []
l.add new Custom(val1: 'ONE', val2: 'one', val3: 'description')
// can be add more values
baseJob(type: 'release', name: 'test', l)

希望对您有所帮助

您只需要将您正在调用的闭包的委托设置为您所在的闭包的委托:

def baseJob(Map m, Closure c = {}) {
    type = m.type ?: 'dev'
    pipelineJob("prefix-${m.name}") {
        parameters {
            stringParam('ONE', 'one', 'Description one')
        }
        c.delegate = delegate // <-- Just add this line
        c()
    }
}

baseJob(type: 'release', name: 'test') {
    parameters {
        stringParam('TWO', 'two', 'Description two')
    }
}

delegate 包含当前正在执行的闭包的委托。