Munish Prabhu
Munish Prabhu

Reputation: 49

How to load a Groovy File and call a method in it via Jenkins Declarative Pipeline Script

I have Jenkinsfile as below in Declarative Pipeline format.

pipeline{
    agent{
        label 'xyz-test'
    }
    options{
        timeout(time: 1, unit: 'HOURS')
        timestamps()
    }
    environment {
        UTILITY = load pwd() + 'path to /utils.groovy'
    }
    stages{
        stage('Build'){
            steps{
                sh "${UTILITY.functionX()}"
            }
        }
    }
}

I have a groovy file with name utils.groovy

def functionX(){
}

def functionY(){
}

return this;

When I call functionX() in such a way, I am getting below error in the console.

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method

Upvotes: 1

Views: 7624

Answers (1)

EricD
EricD

Reputation: 316

you need to use 'load' steps from 'Pipeline: Groovy' plugin (https://plugins.jenkins.io/workflow-cps) in a script block. (https://jenkins.io/doc/pipeline/steps/workflow-cps/#-load-evaluate-a-groovy-source-file-into-the-pipeline-script)

ex:

stages {
    stage ("load scripts"){
        steps {
            script {
                scripts=load "jenkins/scripts/loadScripts.groovy"
            }
        }
    }

Upvotes: 1

Related Questions