我正面临着一个我不明白的问题,我写了一个简单的groovy脚本,当从命令行调用它时,它可以按预期工作
#!/usr/bin/env groovy
def jsonParse(def json) {
new groovy.json.JsonSlurperClassic().parseText(json)
}
def ticketNumbers = ["MYSTATS-2695", "MYSTATS-2694"]
ArrayList<String> jiraLinks = new ArrayList<String>();
for(int i =0; i < ticketNumbers.size(); i++) {
def jira_json = "curl -o /dev/null -X GET -H Content-Type: application/json --cert-type PEM --key-type PEM -E /Users/Jenkins/.jenkins/workspace/certificates/cert.pem --key /Users/Jenkins/.jenkins/workspace/certificates/cert.pem https://jira.dev.org.co.uk:443/rest/api/2/issue/${ticketNumbers[i]}".execute().text;
def json = jsonParse(jira_json);
def summary = json['fields']['summary'].toString();
jiraLinks.add("[" + ticketNumbers[i] + "](https://jira.dev.org.co.uk/browse/" + ticketNumbers[i] + ")" + " - " + summary);
}
println "${jiraLinks}"
所以当我做groovy myscript.groovy
时
这将打印出来
[[MYSTATS-2695 ](https://jira.dev.org.co.uk/browse/MYSTATS-2695 ) - Jenkins build pipeline should ignore draft and pre-releases, [MYSTATS-2694 ](https://jira.dev.org.co.uk/browse/MYSTATS-2694 ) - Android Jenkins pipeline should populate the comscore SDK version automatically]
所以这是意料之中的。
然后,我有一个groovy脚本,我调用它作为jenkins管道构建的一部分
class Helpers {
def jsonParse(def json) {
new groovy.json.JsonSlurperClassic().parseText(json)
}
def createJiraLinks(def ticketNumbers) {
ArrayList<String> jiraLinks = new ArrayList<String>();
for(int i =0; i < ticketNumbers.size(); i++) {
def jira_json = "/usr/bin/curl -o /dev/null -X GET -H Content-Type: application/json --cert-type PEM --key-type PEM -E /Users/Jenkins/.jenkins/workspace/certificates/cert.pem --key /Users/Jenkins/.jenkins/workspace/certificates/cert.pem https://jira.dev.org.co.uk:443/rest/api/2/issue/MYSTATS-2695".execute().text;
def json = jsonParse(jira_json);
def summary = json['fields']['summary'].toString();
jiraLinks.add("[" + ticketNumbers[i] + "](https://jira.dev.org.co.uk/browse/" + ticketNumbers[i] + ")" + " - " + summary);
}
return jiraLinks;
}
}
return new Helpers();
作为Jenkins构建的一部分,我有
def groovyMethod = load("${env.WORKSPACE}/groovy_scripts/release_pipeline.groovy")
def jira = groovyMethod.createJiraLinks(ticketNumberCommits);
echo "JIRA LINKAS ARE $jira"
// $jira is always returned as empty string
我是不是误解了什么,因为我本来希望这样做的?但是看起来curl请求从来没有得到任何东西
谢谢
1条答案
按热度按时间hkmswyz61#
groovy String.execute()返回可能仍在运行的Process(取决于系统负载和天气)
如果您想等待进程结束,请执行以下操作:
对于
jenkins pipeline
,要避免错误java.io.NotSerializableException
,请使用以下代码: