jenkins-groovyjenkins-shared-libraries

Jenkins shared-library: custom step


Following this section of jenkins documentation, I’ve defined a global var in:

my-shared-library/vars/unLabel.groovy

The library is loaded implicitly and the code is:

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

Next from gui I created a pipeline and I choosed "Pipeline script", in the text area I wrote:

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

I got this exception:

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$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.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.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)

Even if I call:

unLabel "test1", "test2" 

or

unLabel("test1","test2")

or

unLabel 'test1', 'test2'

it doesn’t work.

I've tried to access shared-library from a pipeline defined using dsl plugin, here is the definition:

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

This did not works too.

During my attempts I made a syntax error and in the returned exception I could notice that the custom step is defined. This is the exception:

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

So what is wrong here, can someone help me, please?


Solution

  • Your following code:

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

    Is not a valid scripted pipeline syntax nor a declarative pipeline syntax.
    See the Documentation for more details and differences between the two.

    To solve it change you code to one of the following options:

    1. Scripted syntax without an agent (node) context:
      unLabel('test1','test2')
    
    1. Scripted syntax with an agent (node) context:
      node('my-agent'){
          unLabel('test1','test2')
      }
    
    1. Declarative syntax without an agent (node) context:
       pipeline {
           agent none 
           stages {
               stage('Your Stage') {
                  steps {
                     script { // Needed as calling a self defined function
                        unLabel('test1','test2')
                     }
                  }
               }
            }
         }
    
    1. Declarative syntax with an agent (node) context:
       pipeline {
           agent { label 'my-agent' } 
           stages {
               stage('Your Stage') {
                  steps {
                     script { // Needed as calling a self defined function
                        unLabel('test1','test2')
                     }
                  }
               }
            }
         }