简体   繁体   中英

groovy command curl on windows Jenkins

I have a groovy script that work on Linux Jenkins

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;
} 

This script return all the names from Jira jobs by the API , But when I tried to run this groovy on Windows Jenkins the script will not work because windows do not have the command 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;

how should I preform this command?

The following code:

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

is pure groovy, ie no need for curl. It gets the items from the jira instance and returns a List<String> of summaries. Since we don't want any external dependencies like HttpBuidler (as you are doing this from jenkins) we have to manually do the basic auth encoding.

Script tested (the connecting and getting json part, did not test the extraction of summary fields) with:

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

against an atlassian on demand cloud instance.

I removed your jql query as it didn't work for me but you should be able to add it back as needed.

Install curl and set the path in environment variable of windows.

Please follow the link to download curl on windows .

I would consider using HTTP request plugin when making HTTP Requests. Since you are using a plugin, it does not matter if you are running in Windows or . Linux as your Jenkins Host

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM