jenkins 从命令行和通过Groovy脚本执行Curl请求

jgzswidk  于 2023-03-29  发布在  Jenkins
关注(0)|答案(1)|浏览(304)

我正面临着一个我不明白的问题,我写了一个简单的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请求从来没有得到任何东西
谢谢

hkmswyz6

hkmswyz61#

groovy String.execute()返回可能仍在运行的Process(取决于系统负载和天气)
如果您想等待进程结束,请执行以下操作:

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()
    }

相关问题