我希望能够在 Jenkins 中包装一个“阶段”,这样我就可以在阶段的开始和结束时执行自定义代码,例如:
myStage('foo') {
}
Run Code Online (Sandbox Code Playgroud)
我想我可以通过使用元类来做到这一点:
//Wrap stages to automatically trace
def originalMethod = this.metaClass.getMetaMethod("stage", null)
this.metaClass.myStage = { args ->
println "Beginning of stage"
println "Args: " + args
def result = originalMethod.invoke(delegate, args)
println "End of stage"
return result
}
Run Code Online (Sandbox Code Playgroud)
但看起来 Groovy 脚本本身是一个 Binding,它没有元类:
groovy.lang.MissingPropertyException: No such property: metaClass for class: groovy.lang.Binding
Run Code Online (Sandbox Code Playgroud)
我仍在学习 Groovy 和 Jenkins Pipeline 的工作原理,所以也许我只是错过了一些东西。