2

我正在尝试将使用 Jenkins Job DSL 插件的代码分解为可重用的部分,我怀疑我的问题是 Groovy 通用的,而不是 Jenkins 特有的。例如,我想重用这个块的一部分:

freeStyleJob() {
    //generic stuff
    name "something"
    description "something else"

    //custom stuff
    scm {
       svn {
           //etc....
       }
    }
}

通过将名称和描述放在实用方法中(显然我想做的不仅仅是在现实生活中)。但是,我找不到为当前范围创建闭包的正确语法。这是我认为它应该看起来的样子:

def jobCommonItems() {
    return {
        //generic stuff
        name "something"
        description "something else"
    }
}


freeStyleJob() {
    jobCommonItems().call()

    //custom stuff
    scm {
       svn {
           //etc....
       }
    }
}

(也许有一个closure.delegate = this 某处)

但是,这不适用于关闭。它适用于方法,如下所示:https ://dzone.com/articles/groovy-closures-owner-delegate

为了说明,这里有一个测试显示了三种可能的语法组合:

String myString = "Top Level: string"
def myMethod() {
    println "Top Level: Method"
}
def myClosure = { println "Top Level: Class"}

class MyClass1 {
    String myString = "Class1: String"
    def myMethod() {
        println "Class1: Method"
    }
    def myClosure = { println "Class1: Closure"}
}

class MyClass2 {
    String myString = "Class2: String"
    def myMethod() {
        println "Class2: Method"
    }
    def myClosure = { println "Class2: Closure"}
}

class MyClass {
    def closure = {
        println "In-Class generated closure begins, delegate is ${delegate}"
        myMethod()
        myClosure()
        println myString
    }
}

def closure = new MyClass().closure
closure.delegate = new MyClass1()
closure()

closure = new MyClass().closure
closure.delegate = new MyClass2()
closure()

// This fails - it can find the top level method, but not closure or string
closure.delegate = this
closure()



def methodMissing(String methodName, args) {
    println "Method not found in class ${this} by name ${methodName}"
}

我收到一个错误,即闭包不在主类中(即测试 test.groovy):在类 test@60611244 中找不到方法,名称为 myClosure

我尝试将委托更改为“this”,尝试更改查找策略等。我可能缺少一些基本的东西。

4

4 回答 4

8

作业工厂方法freeStyleJob返回一个对象,该对象可用于使用该with方法应用更多配置。该方法需要一个闭包参数,该参数与传递给该方法的闭包具有相同的属性freeStyleJob

def basicConfiguration() {
    return {
        description('foo')
        scm {
            // whatever
        }
    }
}

def myJob = freeStyleJob('example') {
    publishers {
        // more config
    }
}
myJob.with basicConfiguration()

脚本本身是一个实例,DslFactory它是包含例如freeStyleJob方法的接口。您可以将该对象传递给类或方法以使用freeStyleJob.

def myJobFactory(def dslFactory, def jobName) {
    dslFactory.freeStyleJob(jobName) {
        description('foo')
    }
}

def myJob = myJobFactory(this, 'example')

然后您可以使用该myJob对象通过with.

于 2015-09-09T07:29:10.860 回答
2

似乎一种解决方案是像这样反转关系,并将“this”作为上下文传递以查找 DSL 顶级闭包。

class Utils {
    static def makeMeABasicJob(def context) {
        context.freeStyleJob() {
            //generic stuff
            name "something"
            description "something else"
        }

    }
}

def job1 = Utils.makeMeABasicJob(this) //Passing the groovy file class as the resolution context
job1.with({
    //custom stuff
    scm {
        svn {
            //etc....
        }
    }
})
于 2015-09-08T19:53:20.627 回答
0

当试图从我的 Jenkins DSL groovy 脚本中提取通用逻辑时,我最初使用将作业对象传递给通用静态帮助器的样式,然后执行job.with{...}类似于 daspilker 的解决方案。

但是,我发现使用代表更直接一些。例如:

freeStyleJob('name') {
    jobDesc(delegate, 'something')
    jobSCM(delegate, 'git@gitlab.com:MyGroup/MyProject.git', 'master')
}

def jobDesc(def context, def descText) {
    context.description(descText)
}

def jobSCM(def context, def repoURL, def branchName)
{
    context.scm {
        git {
                remote {
                    url(repoURL)
                }
                branch('refs/heads/' + branchName)
        }   
    }
}

jobDesc和jobSCM方法可以作为静态助手移动到单独的实用程序类中,然后您可以将其导入到您的 DSL groovy 脚本中

于 2021-01-22T10:35:12.923 回答
0

这样的事情怎么样?

def jobCommonItems(job) {
    job.name = "something"
    job.description = "something else"
}

freeStyleJob() {
    jobCommonItems(this)

    //custom stuff
    scm {
       svn {
           //etc....
       }
    }
}
于 2015-09-08T20:09:28.907 回答