脚本化管道:包装阶段 [英] Scripted pipeline: wrap stage

查看:77
本文介绍了脚本化管道:包装阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Jenkins中包装一个阶段",因此我可以在阶段的开始和结束时执行自定义代码,如下所示:

I would like to be able to wrap a 'stage' in Jenkins, so I can execute custom code at the start and end of a stage, so something like:

myStage('foo') {
}

我认为我可以使用metaClass做到这一点:

I thought I could do this by using metaClass:

           //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
           }

但是看来Groovy脚本本身是一个Binding,它没有metaClass:

But it appears the Groovy script itself is a Binding, which doesn't have a metaClass:

groovy.lang.MissingPropertyException: No such property: metaClass for class: groovy.lang.Binding

我仍在学习Groovy和Jenkins Pipeline的工作方式,所以也许我只是缺少一些东西.

I'm still learning how Groovy and Jenkins Pipeline work, so perhaps I'm just missing something.

推荐答案

我不熟悉元类的概念,但我认为,解决您的问题的简单方法是将包装阶段定义为函数. 这是如何定义这样的函数的示例:

I am not familiar with the metaclass concept but I think that a simple solution to your problem is to define a wrapped stage as a function. Here's an example of how you'd define such a function:

def wrappedStage(name, Closure closure) {
    stage(name) {
        echo "Beginning of stage"
        def result = closure.call()
        echo "End of stage"
        return result
    }
}

这就是您的称呼方式:

wrappedStage('myStage') {
    echo 'hi'
}

wrappedStage的返回值仅在舞台的主体实际返回某些内容时才有意义,例如:

The return value of wrappedStage would only make sense when the body of your stage actually returns something, for example:

  1. 如果您要调另一个职位,例如:

  1. If you call another job, eg:

wrappedStage('myStage') {
    build job: 'myJob'
}

您将返回org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper,可用于访问您正在运行的作业的信息,例如结果,变量等

you will get back org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper which you can use to access info of the job you run, like result, variables etc

如果您在控制台上打印一些内容,例如:

If you print something to the console, eg:

wrappedStage('myStage') {
    echo 'hi'
}

您将返回null.

请注意,在我的示例中,我不打印args,因为我理解stage的方式仅包含2个参数.阶段名称及其应运行的闭包.阶段的名称已经在日志中打印了,我不知道从打印将要执行的代码中将获得多少价值,但是如果您要这样做,请看一下.

Note that in my example I am not printing args because the way I understand stage, it only takes 2 arguments; the stage name and the closure it should run. The name of the stage will already be printed in the log, and I don't know how much value you'd get from printing the code you're about to execute but if that's something you want to do, take a look at this.

如果您要包装的内容有更具体的情况,可以在包装器中添加更多参数,并通过这些额外的参数打印所需的所有信息.

If you have a more specific case in mind for what you'd want to wrap, you can add more params to the wrapper and print all the information you want through those extra parameters.

这篇关于脚本化管道:包装阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆