Jenkins 共享库:自定义步骤

Jenkins shared-library: custom step

根据 jenkins 文档的 this section,我在以下位置定义了一个全局变量:

my-shared-library/vars/unLabel.groovy

隐式加载库,代码为:

def call(String labelName, String tfsPath) {
    echo "Hello, ${labelName} ${tfsPath}"             
}

接下来从 gui 我创建了一个管道,我选择了“管道脚本”,在我写的文本区域:

label 'my-agent'
unLabel('test1','test2')

我遇到了这个异常:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.cps.UninstantiatedDescribableWithInterpolation.unLabel() is applicable for argument types: (java.lang.String, java.lang.String) values: [test, test1]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at WorkflowScript.run(WorkflowScript:2)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    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.GeneratedMethodAccessor670.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.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable.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.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
    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)

即使我打电话:

unLabel "test1", "test2" 

unLabel("test1","test2")

unLabel 'test1', 'test2'

没用。

我尝试从使用 dsl 插件定义的管道访问共享库,定义如下:

pipelineJob('tfs-unlabel') {
        description('delete label')
        parameters {
             stringParam('LABEL_NAME', '', 'The label name that will be removed')
             stringParam('TFS_PROJ_FOLDER', '', 'The TFS project path where label will be removed)
        }
        definition {
            cpsFlowDefinition {    
                script("label 'maven-agent' unLabel params.LABEL_NAME, params.TFS_PROJ_FOLDER")    
                sandbox(false)
            }
        } 
}

这个也不行。

在我尝试的过程中,我犯了一个语法错误,在返回的异常中我注意到自定义步骤已定义。这是例外情况:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps [....] or globals [..., env, params, pipeline, scm, unLabel]

这里有什么问题,有人可以帮助我吗?

您的以下代码:

label 'my-agent'
unLabel('test1','test2')

既不是有效的脚本化管道语法,也不是声明性管道语法。
有关详细信息和两者之间的差异,请参阅 Documentation

要解决此问题,请将代码更改为以下选项之一:

  1. 没有代理(节点)上下文的脚本语法:
  unLabel('test1','test2')
  1. 带有代理(节点)上下文的脚本语法:
  node('my-agent'){
      unLabel('test1','test2')
  }
  1. 没有代理(节点)上下文的声明语法:
   pipeline {
       agent none 
       stages {
           stage('Your Stage') {
              steps {
                 script { // Needed as calling a self defined function
                    unLabel('test1','test2')
                 }
              }
           }
        }
     }
  1. 带有代理(节点)上下文的声明语法:
   pipeline {
       agent { label 'my-agent' } 
       stages {
           stage('Your Stage') {
              steps {
                 script { // Needed as calling a self defined function
                    unLabel('test1','test2')
                 }
              }
           }
        }
     }