来自命令行和Groovy脚本的卷曲请求

时间:2017-06-05 15:34:41

标签: curl jenkins groovy jenkins-pipeline

我正面临一个我不明白的问题,我写了一个简单的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

我是否误解了这里的任何内容,因为我本以为这会起作用?但似乎卷曲请求永远不会得到任何回报

由于

1 个答案:

答案 0 :(得分:1)

groovy String.execute()返回可能仍在运行的Process(取决于系统负载和天气))

如果您想等到流程结束,请执行以下操作:

if (decimal.TryParse(priceLabel2.Text, out decimal price))
{
    total += price;
}
else
{
    MessageBox.Show("Input is not a valid decimal.");
}

def txt = "cmd /c dir c:\\".execute().with{ def output = new StringWriter() def error = new StringWriter() //wait for process ended and catch stderr and stdout it.waitForProcessOutput(output, error) //check there is no error assert error.toString().size()==0: "$error" //println it.exitValue() //we can do check with error code //return stdout from closure return output.toString() } 以避免错误jenkins pipeline 使用以下代码:

java.io.NotSerializableException