一尘不染

通过命令行和Groovy脚本卷曲请求

jenkins

我遇到一个我不明白的问题,我编写了一个简单的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();

作为我的詹金斯建筑的一部分,我有

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请求永远都没有回来

谢谢


阅读 293

收藏
2020-07-25

共1个答案

一尘不染

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

如果要等到过程结束,请执行以下操作:

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 使用以下代码:

node {
    def res = runAndWait("cmd /c dir c:\\")
    echo res
}

@NonCPS
String runAndWait(Object cmd){
    def proc = cmd.execute()
    def output = new StringWriter()
    def error = new StringWriter()
    //wait for process ended and catch stderr and stdout 
    proc.waitForProcessOutput(output, error)
    //check there is no error
    assert error.toString().trim().size()==0: "$error"
    //assert proc.exitValue()==0 //we can do check with error code
    //return stdout from closure 
    return output.toString()
}
2020-07-25