简体   繁体   English

如何加载 Groovy 文件并通过 Jenkins 声明式管道脚本调用其中的方法

[英]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.我有如下声明性管道格式的 Jenkinsfile。

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我有一个名为utils.groovy的 groovy 文件

def functionX(){
}

def functionY(){
}

return this;

When I call functionX() in such a way, I am getting below error in the console.当我以这种方式调用 functionX() 时,控制台中出现以下错误。

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

you need to use 'load' steps from 'Pipeline: Groovy' plugin ( https://plugins.jenkins.io/workflow-cps ) in a script block.您需要在脚本块中使用“管道:Groovy”插件( https://plugins.jenkins.io/workflow-cps )中的“加载”步骤。 ( https://jenkins.io/doc/pipeline/steps/workflow-cps/#-load-evaluate-a-groovy-source-file-into-the-pipeline-script ) 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"
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM