jenkins-pipelinejenkins-groovy

Jenkins "java.lang.NoSuchMethodError: No such DSL method" for function defined in groovy lib


Pipeline:


// ./vars/tools.groovy
@Library('my-lib') _

pipeline {
  ...
  post {
    failure {
      script {
        tools.findErrors(currentBuild.rawBuild)
      }
    }
  }
}

tools.groovy:

def action1(rawBuild)
{
  println("-- action1 --")
  def res = []
  ...
  res.each{ println(it) }
  return res
}

def action2(List res, String line)
{
  println("-- action2 --")
  ...
}

def findErrors(rawBuild)
{
  def errors = action1(rawBuild)
  def res = []
  errors.each{ action2(res, it) }
}

The output:

-- action1 --
error: ...
error: ...

Error when executing failure post condition:
Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: some_guid
java.lang.NoSuchMethodError: No such DSL method 'action2' found among steps [archive, bat, build ....]
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:219)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
    at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1295)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1184)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:41)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at org.jenkinsci.plugins.workflow.cps.LoggingInvoker.methodCall(LoggingInvoker.java:105)
    at tools.findErrors(tools.groovy:1111)

How to fix this?


Solution

  • As suggested in this answer, I changed action2 to:

    def action2(res, line)
    {
      println("-- action2 --")
      ...
    }
    

    And exception is gone.