groovy.lang.MissingMethodException:httpRequest()方法的签名不适用于参数类



我有以下一段代码,它们曾经在管道中工作得很好。我必须把它移到Jenkins的一个共享库中,所以为它创建了一个类,并进行了必要的调整。

def toJson (input) {
return groovy.json.JsonOutput.toJson(input)
}
def void callAPI (args) {
def apiRequestBody = [
"prop1": args.param1,
"prop2": args.param2
]
// Build the request - notice that authentication should happen seamlessly by using Jenkins Credentials
response = httpRequest (authentication: "${CREDENTIALS_STORED_IN_JENKINS}",
consoleLogResponseBody: true,
httpMode: 'POST',
requestBody: toJson(apiRequestBody),
url: "${API_URL}",
customHeaders: [
[
name: 'Content-Type',
value: 'application/json; charset=utf-8'
],
[
name: 'Accept-Charset',
value: 'utf-8'
]
]
)

当我调用callAPI (args)方法时,我得到以下错误:

Exception groovy.lang.MissingMethodException:没有方法的签名:MY_PACKAGE_PATH。MY_CLASS.httpRequest((适用于参数类型:(java.util.LinkedHashMap(值:[[authentication:MAPI_UID_PW,consoleLogResponseBody:true,…]]

我缺少什么?

感谢

httpRequest是一个DSL命令,在类的上下文中不能直接使用,就像不能使用shbatnode一样。看见https://www.jenkins.io/doc/book/pipeline/shared-libraries/#accessing-有关此的更多信息,请参阅步骤。

您可以从Jenkinsfile中提取代码,并将其放置在";var";(或全局变量(。如果您坚持将代码放在共享库类中,请参阅上面的链接,它将把您的代码转换为(注意"script"参数和script.httpRequest语法(:

def void callAPI (script, args) {
def apiRequestBody = [
"prop1": args.param1,
"prop2": args.param2
]
// Build the request
response = script.httpRequest (authentication: "${CREDENTIALS_STORED_IN_JENKINS}",
// ...
}

最新更新