Search code examples
groovyjenkins-pipelinedsl

jenkinsfile - check if method is defined


I am trying to execute a stage only if a function exists:

if (binding.variables.containsKey(functionName)){
    stage ("execute") {
      script{
        "${functionName}"(config)
      }
    }
  }

the problem is that binding.variables return only variables (I should have guessed that). is there a way to check if a function is defined?


Solution

  • MethodNotFound is a checked Exception in the Exception Hierarchy and hard to capture in groovy or Java flow with try..catch block, so it hard to achieve your goal

    enter image description here

    If the method or DSL is not defined in Jenkins, Jenkins will through an exception while parsing the scripted or declarative pipeline - In that case, I don't think you can capture it ever