来自命令行和Groovy脚本的curl请求 [英] Curl request from command line and via Groovy script
问题描述
我遇到了一个我不明白的问题,我编写了一个简单的groovy脚本,当它从命令行调用时能够按预期运行
#!/ usr / bin / env groovy
$ b $ 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] +)+ - +摘要);
}
println$ {jiraLinks}
so当我做> groovy myscript.groovy
这会打印出来
[[MYSTATS-2695](https://jira.dev.org.co.uk/browse/MYSTATS-2695) - Jenkins构建管道应忽略draft和pre-发布,[MYSTATS-2694](https://jira.dev.org.co.uk/browse/MYSTATS-2694) - Android Jenkins管道应自动填充comscore SDK版本]
所以这是预期的。
然后我有一个groovy脚本, call作为jenkins管道构建的一部分
class Helpers {
def jsonParse(def json){
new groovy.json.JsonSlurperClassic()。parseText(json)
}
$ b $ def 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-类型: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] +)+ - +摘要);
}
返回jiraLinks;
}
}
返回新的Helpers();
作为Jenkins构建的一部分,我有
def groovyMethod = load($ {env.WORKSPACE} /groovy_scripts/release_pipeline.groovy)
def jira = groovyMethod.createJiraLinks(ticketNumberCommits);
echoJIRA LINKAS ARE $ jira
// $ jira总是作为空字符串返回
我误解了这里的任何内容,因为我预计这会起作用?但似乎curl请求永远不会收回任何东西
谢谢
解决方案groovy String.execute()返回进程可能还在运行(取决于系统负载和天气))
如果您想等到进程结束,请执行以下操作:
def txt =cmd / c dir c:\\.execute()。with {
def output = new StringWriter )
def error = new StringWriter()
//等待进程结束并捕获stderr和stdout
it.waitForProcessOutput(输出,错误)
//检查没有错误
assert error.toString()。size()== 0:$ error
// println it.exitValue()//我们可以检查错误代码
//返回stdout from
return output.toString()
}
for
jenkins pipeline
到避免错误java.io.NotSerializableException
使用以下代码:
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()
//等待为进程结束并捕获stderr和stdout
proc.waitForProcessOutput(输出,错误)
//检查没有错误
assert error.toString()。trim()。size()= = 0:$ error
//断言proc.exitValue()== 0 //我们可以检查错误代码
//从闭包返回stdout
return output.toString( )
}
I am facing an issue which i do not understand, I have written a simple groovy script that when called from the command line works as expected
#!/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}"
so when i do
groovy myscript.groovy
This will print out
[[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]
So that is as expected.
What i then have is a groovy script which i call as part of a jenkins pipeline build
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();
As part of my Jenkins build i have
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
Am i misunderstanding anything here as i would have expected this to work? but it seems as if the curl request never gets anything back
Thanks
解决方案groovy String.execute() returns Process that could be still running (depends on system load and weather))
if you want to wait until process ended do like this:
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() }
for
jenkins pipeline
to avoid errorjava.io.NotSerializableException
use the following code: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() }
这篇关于来自命令行和Groovy脚本的curl请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!