首页 > 解决方案 > windows Jenkins上的groovy命令curl

问题描述

我有一个适用于 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 上运行这个 groovy 时,该脚本将不起作用,因为 Windows 没有命令 curl

def artifactsObjectRaw = [" curl ", "-u","someusername@xxxx.com:tokenkey" ,"-X" ,"GET", "-H", "Content-Type: application/json", "-H ", "accept: application/json","-K","--url","${artifactsUrl}"].execute().text;

我应该如何执行这个命令?

标签: windowsjenkinsgroovy

解决方案


以下代码:

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

是纯粹的 groovy,即不需要 curl。它从 jira 实例中获取项目并返回List<String>摘要。由于我们不想要任何像 HttpBuidler 这样的外部依赖项(就像你从 jenkins 做的那样),我们必须手动进行基本的身份验证编码。

脚本测试(连接和获取 json 部分,没有测试summary字段的提取):

Groovy Version: 2.4.15 JVM: 1.8.0_201 Vendor: Oracle Corporation OS: Linux

针对 atlassian 按需云实例。

我删除了您的 jql 查询,因为它对我不起作用,但您应该能够根据需要将其添加回来。


推荐阅读