简体   繁体   中英

How to fix 'hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: testFunc.call() '

I am trying to call a function using shared library in Jenkins. The shared library groovy file exists in the /vars folder of a repository.

I have tried othernames for the shared library file. I tried calling it also using 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'
                }
}

Jenkinsfile that uses the above shared librabry

@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'])
                }
                  }
            }   
    }

Error: hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: testFunc.call() is applicable for argument types: () values: []

In your testFunc.groovy you need to rename function from testFunc into call(). After this changes it will be possible to call your function as declarative step

And in my case I had

call() {

while it should be

def call() {

Shame.

I added this to my groovy and it worked for me. Dont know if this helps!

Solution

def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

pipeline{
//your code
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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