1

I have a Jenkins Pipeline JOB where I declared some stages that use an external function that I created by myself in the same groovy script.

errorList = ["badGatewayMsg", "closedByRemoteHostMsg", "connectionTimedOut"]
def boolean someFunction(name) {
    String jobLog = jenkins.model.Jenkins.instance.getItemByFullName(name).lastBuild.log
    for (error in errorList) {
        if (jobLog.contains(error))
            return true
    }
    return false
}

stage('stage1') {
        if(someFunction('job1Name'))
           // do Something
    }

stage('stage2') {
        if(someFunction('job2Name'))
           // do Something
    }

When I want to start this pipeline build i get the following error:

java.lang.NoSuchMethodError: No such DSL method 'someFunction' found among steps ....

Thanks for your help!

marherbi
  • 321
  • 3
  • 15
  • 1
    Is the code redacted or it really reports a method never used or declared in your code? Also, full stacktrace of the exception would help. – Oliver Gondža Aug 03 '18 at 10:47
  • The method is really used in all of my stages, I just don't know where I can declare it to be visible in all my groovy script – marherbi Aug 03 '18 at 10:58
  • @marherbi I do not think you understood his question. You are invoking a method you have not declared or defined in the code shown. He is asking if you did not display the code or it does not exist, in which case that is the problem. – Matt Schuchard Aug 03 '18 at 12:53
  • The method you seem to be using is `someFunction` but the exception complains about `isOnError` which does not seem to be an internal identifier based on causal search. – Oliver Gondža Aug 03 '18 at 13:18
  • Ah!!! you are right, I just changed the method name, sorry!! – marherbi Aug 03 '18 at 13:59
  • Not sure whether the issue still exists. Could it be because you’re using both ‘def’ and the datatype (boolean) when declaring that method? However I would expect a different error, though. – Joerg S Aug 04 '18 at 20:13
  • @Joerg S, you mean that I have to ommit the 'def' keyword ? I think that the problem persist because this method is not visible in the stages and I want to know how to write a whole script where my method can be visible everywhere – marherbi Aug 05 '18 at 13:18
  • Did you try to omit the def keyword yet? Not sure if this is even valid groovy syntax. Either use def or a datatype but not both. – Joerg S Aug 11 '18 at 14:11

1 Answers1

1

Out of curiosity I copied the code into my local Jenkins - and it worked (after fixen obvious issues like creating the missing jobs and fixing the if conditions).

Nevertheless to get the code clean you'll need to:

  1. Get rid of the def keyword (or alternatively get rid of the data type definition boolean). You might want to check: Groovy: "def" keyword vs concrete type

  2. Add the @NonCPS keyword when accessing Jenkins internals which are not serializable.

  3. For completeness: In addition to that of course for accessing the Jenkins internals you need to switch of the sandbox mode or put your code into a global shared library.

Here's my working example:

errorList = ["badGatewayMsg", "closedByRemoteHostMsg", "connectionTimedOut"]
@NonCPS
boolean someFunction(name) {
    String jobLog = jenkins.model.Jenkins.instance.getItemByFullName(name).lastBuild.log
    for (error in errorList) {
        if (jobLog.contains(error))
            return true
    }
    return false
}

stage('stage1') {
    if(someFunction('job1Name')) {
       // do Something
    }
}

stage('stage2') {
    if(someFunction('job2Name')) {
       // do Something
    }
}
Joerg S
  • 3,440
  • 2
  • 15
  • 36