我有一个可在Linux Jenkins上运行的groovy脚本
import groovy.json.JsonSlurper
try {
List<String> artifacts = new ArrayList<String>()
//jira get summery for list by issue type story and label demo and project 11411
def artifactsUrl = 'https://companyname.atlassian.net/rest/api/2/search?jql=project=11411%20and%20issuetype%20in%20(Story)%20and%20labels%20in%20(demo)+&fields=summary' ;
def artifactsObjectRaw = ["curl", "-u", "someusername@xxxx.com:tokenkey" ,"-X" ,"GET", "-H", "Content-Type: application/json", "-H", "accept: application/json","-K", "--url","${artifactsUrl}"].execute().text;
def parser = new JsonSlurper();
def json = parser.parseText(artifactsObjectRaw );
//insert all result into list
for(item in json.issues){
artifacts.add( item.fields.summary);
}
//return list to extended result
return artifacts ;
}catch (Exception e) {
println "There was a problem fetching the artifacts " + e.message;
}
此脚本通过API返回Jira作业中的所有名称, 但是,当我尝试在Windows Jenkins上运行这种常规操作时,该脚本将无法运行,因为Windows没有命令curl
def artifactsObjectRaw = [“ curl ”,“ -u”,“ someusername@xxxx.com:tokenkey”,“-X”,“ GET”,“ -H”,“ Content-类型:application / json“,”-H“,” accept:application / json“,”-K“,”-url“,” $ {artifactsUrl}“]。execute()。text;
我应该如何执行此命令?
答案 0 :(得分:2)
以下代码:
import groovy.json.JsonSlurper
try {
def baseUrl = 'https://companyname.atlassian.net'
def artifactsUrl = "${baseUrl}/rest/api/2/search?jql=project=MYPROJECT&fields=summary"
def auth = "someusername@somewhere.com:tokenkey".bytes.encodeBase64()
def headers = ['Content-Type': "application/json",
'Authorization': "Basic ${auth}"]
def response = artifactsUrl.toURL().getText(requestProperties: headers)
def json = new JsonSlurper().parseText(response)
// the below will implicitly return a list of summaries, no
// need to define an 'artifacts' list beforehand
def artifacts = json.issues.collect { issue -> issue.fields.summary }
} catch (Exception e) {
e.printStackTrace()
}
是纯常规的,即不需要卷曲。它从jira实例获取项目并返回List<String>
的摘要。由于我们不需要像HttpBuidler这样的外部依赖项(就像您在jenkins上所做的那样),我们必须手动执行基本的auth编码。
使用以下命令测试脚本(连接和获取json部分,未测试summary
字段的提取)
Groovy Version: 2.4.15 JVM: 1.8.0_201 Vendor: Oracle Corporation OS: Linux
针对按需Atlassian云实例。
我删除了您的jql查询,因为它对我不起作用,但是您应该能够根据需要将其添加回去。
答案 1 :(得分:0)
安装curl并在Windows的环境变量中设置路径。
请点击链接以下载windows上的curl。
答案 2 :(得分:0)
我会在发出HTTP请求时考虑使用HTTP请求插件。 由于您使用的是插件,因此无论您是在Windows还是Windows中运行都没有关系。 Linux as your Jenkins Host