Reputation: 61
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: []
Upvotes: 6
Views: 51273
Reputation: 1
in shared librarian, under vars directory file is test.groovy
the pipeline code should start like below
def call () {
pipeline {
agent any
stages {
stage('stage_name') {
}
}
}
}
Upvotes: 0
Reputation: 11
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
}
}
Upvotes: 0
Reputation: 126
In your testFunc.groovy
, you need to rename the function from testFunc
into call()
.
After these changes, it will be possible to call your function as declarative step.
Upvotes: 2
Reputation: 2672
And in my case I had
call() {
while it should be
def call() {
Shame.
Upvotes: 4