簡體   English   中英

Windows Jenkins 上的常規命令 curl

[英]groovy command curl on windows Jenkins

我有一個適用於 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;

我應該如何執行這個命令?

以下代碼:

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 做的那樣),所以我們必須手動進行基本的身份驗證編碼。

腳本測試(連接和獲取 json 部分,沒有測試summary字段的提取):

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

針對 Atlassian 按需雲實例。

我刪除了您的 jql 查詢,因為它對我不起作用,但您應該可以根據需要將其添加回來。

安裝 curl 並在 windows 的環境變量中設置路徑。

請按照鏈接在windows上下載 curl。

在發出 HTTP 請求時,我會考慮使用 HTTP 請求插件。 由於您使用的是插件,因此無論您是在 Windows 還是 . Linux 作為您的 Jenkins 主機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM