在Jenkins管道中的WorkflowScript上使用.with

时间:2019-10-28 09:14:12

标签: jenkins groovy jenkins-pipeline

我将Jenkins管道脚本的一些公共部分提取到管道库中的帮助器类。例如,每个作业都应使用timestampsansiColor,所以让我们这样写:

class Foo implements Serializable {
  WorkflowScript wfs

  Foo(WorkflowScript wfs) {
    this.wfs = wfs
  }

  def foo(Closure body) {
    this.wfs.timestamps {
      this.wfs.ansiColor {
        body()
      }
    }
  }
}

管道可以像这样使用此类:

def f = new Foo(this)
f.foo { echo "Hello World!" }

这可行,但是在助手类的每个流水线步骤前都加上this.wfs会增加很多噪音。我当时正在考虑编写此代码:

class Foo implements Serializable {
  WorkflowScript wfs

  Foo(WorkflowScript wfs) {
    this.wfs = wfs
  }

  def foo(Closure body) {
    this.wfs.with {
      timestamps {
        ansiColor {
          body()
        }
      }
    }
  }
}

但是,这样作业会失败,并出现以下错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: Foo.timestamps() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2@279d65ed]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:64)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:160)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:142)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:158)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:162)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at Foo.foo(WorkflowScript:146)
    at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.with(CpsDefaultGroovyMethods:242)
    at Foo.foo(WorkflowScript:145)
    at WorkflowScript.run(WorkflowScript:159)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:84)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor188.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:66)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

.with上使用WorkflowScript有什么问题?对于其他类,这似乎很好用。有解决方法吗?

1 个答案:

答案 0 :(得分:0)

如果您要做的只是包装管道,则可以在共享库步骤中实现。假设您创建Foo.groovy

def call(Closure body) {
    pipeline {
        agent any
        stages {
            stage("Initialize") {
                steps {
                    timestamps {
                        ansiColor {
                            echo "Hello world!"
                            body()
                        }
                    }
                }
            }
        }
    }
}

然后,您可以使用Foo代替pipeline

library "your-shared-lib"
Foo {
  echo "123"
}

此示例仅限于一个阶段,但是,如果需要更多阶段,则必须使用脚本化管道。在Foo.groovy中,您需要将body()放在script块中,然后可以添加所需的任何内容:

library "your-shared-lib"
Foo {
  stage("1") {
    echo "Only scripted pipeline here"
  }
  stage("2") {
    // If you plan on using more than one node it's better to
    // change the agent in Foo.groovy to none
    node("label") {
    }
  }
}

此方法的缺点是您不得不使用脚本化管道,并且所有阶段都将嵌套在Foo的父阶段中(但是Blue Ocean足够聪明,可以显示以下阶段:父阶段。)