簡體   English   中英

Jenkins:在 Shell 腳本/CURL 請求中使用變量

[英]Jenkins: Using variable in Shell Script / CURL Request

我將來自較早 HTTP 請求的響應中的數字保存到變量中。 在 CURL 請求中,我需要將此數字插入 URL,但不知何故它沒有被識別為變量。 它剛剛被整理出來,這就是為什么 URL 是錯誤的,我得到一個 400 錯誤。

script{

// first httpRequest where I get the number from
def response = httpRequest httpMode: 'POST', url:"https://myServer.com", contentType: 'APPLICATION_JSON_UTF8', requestBody: 'myBody'

def props = readJSON text: response.content

def num = props.id[0]       //num stores the number now

sh(Script: curl "https://myServer.com/$num/files" -i -X POST -H Content-Type:multipart/form-data -F file=@JenkinsDemo.txt
}

控制台 output 向我顯示 sh 試圖到達https://myServer.com//files ,所以它忽略了變量。

我還嘗試將整個 URL 放入一個變量中並在 curl 中引用,但它也沒有被識別。 當然,我嘗試了幾種輸入選項,例如 ${num}、'$num' 等...

有任何想法嗎?

編輯

我還收到消息“消息”:“給定的 url 包含惡意字符”,可能是因為“$”

另一個奇怪的事情:如果我輸入 ${props.id[0]} 而不是 ${num} (這沒有任何區別,因為它們擁有相同的數字),消息將更改為“/var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: 1: /var/jenkins_home/workspace/Pipeline Test@tmp/durable-153a0829/script.sh: 錯誤替換”

我解決了這個問題。 curl 請求需要用雙引號引起來。

sh("curl https....")

雙引號可以使用 groovy 腳本中定義的變量。 沒有它們,它只會被解釋為 java 字符串。

暫無
暫無

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

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