我有一個 Jenkins 管道,該命令可以在其中運行,并通過 google chat 向我發送通知:
script {
sh 'curl -k "https://chat.googleapis.com/v1/spaces/AAAABHT3HT0/messages?key=*****&token=******" -d "@chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
但是,如果我在變數中輸入 url,那將不再起作用:
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh 'curl -k ${url} -d "@chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
隨著錯誤:
curl -k -d @chat_notification.json -XPOST -H 'Content-Type: application/json; charset=UTF-8'
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
這可能是一個報價問題?
uj5u.com熱心網友回復:
是的,如果您在 Groovy 中使用單引號,這意味著您不能使用$語法插入帶有變數的字串。
請參閱https://groovy-lang.org/syntax.html#_string_interpolation。
順便說一下,或者sh在這種情況下,同樣的事情也適用于 shell 代碼。但是由于您沒有在代碼中使用任何 shell 變數,只需交換引號,即用",參考您的 Groovy 字串,并'在 curl 命令中使用可能會起作用。
uj5u.com熱心網友回復:
是的,這是一個參考問題,嘗試使用""而不是在命令中''使用url變數的值curl。你也應該分開-X和POST(你-XPOST在你的腳本中)
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh "curl -k ${url} -d @chat_notification.json -X POST -H Content-Type: application/json; charset=UTF-8"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371897.html
