如何修复“hudson.remoting.ProxyException:groovy.lang.MissingMethodException:没有方法签名:testFunc.call()”

Yel*_*her 6 groovy shared-libraries jenkins jenkins-pipeline

我正在尝试使用 Jenkins 中的共享库调用函数。共享库 groovy 文件存在于存储库的 /vars 文件夹中。

我尝试过共享库文件的其他名称。我尝试也使用 testFunc.call()、testFunc、testFunc() 来调用它

testFunc.groovy


import hudson.model.*
import hudson.EnvVars
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.*
import java.net.URL
def coveragepercentage
def testFunc(){

    def param = "${env:TestCoverage}"
    echo param
    def paramInt = param as int
    echo "Integer "+ paramInt
    def jsondata = readFile(file:'./target/site/munit/coverage/munit-coverage.json')
           def data = new JsonSlurperClassic().parseText(jsondata)
           coveragepercentage = data.coverage
           echo "${coveragepercentage}"

        if(coveragepercentage > paramInt){
           println("This value is smaller")
           sh "exit 1"
           currentBuild.result = 'FAILURE'
                }
}

Run Code Online (Sandbox Code Playgroud)

使用上述共享库的 Jenkinsfile

@Library('shared-lib2') _
import hudson.model.*
import hudson.EnvVars
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.*
import java.net.URL
pipeline{
  agent any
    tools {
      maven 'Maven'
      jdk  'JAVA'
        }
    stages {
        stage('build') {
            steps {
                configFileProvider([configFile(fileId: 'config', variable: 'MAVEN_SETTINGS_XML')]) {
                sh "mvn -s $MAVEN_SETTINGS_XML clean package"
                }
            }
        }
        stage('test function'){
            steps{
                script{
                testFunc()
                }
            }
        }

        stage('MUnit Test Report') {
               steps{
                script {
                  publishHTML(target:[allowMissing: false,alwaysLinkToLastBuild: true,keepAll: true,reportDir: 'target/site/munit/coverage',reportFiles: 'summary.html',reportName: 'MUnit Test Report',reportTitles: 'MUnit Test Coverage Report'])
                }
                  }
            }   
    }
Run Code Online (Sandbox Code Playgroud)

错误:hudson.remoting.ProxyException:groovy.lang.MissingMethodException:没有方法签名:testFunc.call() 适用于参数类型:() 值:[]

小智 2

在您的 中testFunc.groovy,您需要将函数从 重命名testFunccall()

在这些更改之后,将可以作为声明性步骤调用您的函数。