嘗試從 Groovy 向 GitHub API 發送請求:
def res = null
withCredentials([string(credentialsId: 'my-github-token', variable: 'GITAPITOKEN')]) {
withEnv(["REPO=${repo}", "PRID=${prId}", "LABEL=${label}"]) {
res = sh (script: 'curl -X PUT -H \\"Authorization: token $GITAPITOKEN\\" -d \\"{\\\\"labels\\\\":[\\\\"$LABEL\\\\"]}\\" https://api.github.com/repos/my-user/$REPO/issues/$PRID/labels', returnStdout: true).trim()
}
}
println("${res}")
它表明它執行以下操作:
curl -X PUT -H "Authorization: token ****" -d "{\"labels\":[\"my-label\"]}" https://api.github.com/repos/my-user/my-repo/issues/1/labels
當我在本地運行此命令(包括所有轉義字符)時,它可以完美運行
但是在詹金斯上-這又回來了
curl: (6) Could not resolve host: token
curl: (6) Could not resolve host: ****"
curl: (3) unmatched close brace/bracket in URL position:
my-label\"]}"
^
和:
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/issues#set-labels-for-an-issue"
}
所以似乎標題轉義在某種程度上不起作用 - 我錯過了什么?
uj5u.com熱心網友回復:
試試下面的東西。
pipeline {
agent any
stages {
stage('Hello') {
steps {
script {
def repo = 'test'
def label = "-d \"{\"labels\":[\"label1\"]}"
def prId = "123"
def token = "1234567890"
withEnv(["REPO=${repo}", "PRID=${prId}", "LABEL=${label}", "GITAPITOKEN=${token}"]) {
res = sh (script: 'curl -v -X PUT -H \"Authorization: token $GITAPITOKEN\" $LABEL https://api.github.com/repos/my-user/$REPO/issues/$PRID/labels', returnStdout: true).trim()
echo "$res"
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489572.html
標籤:詹金斯 卷曲 时髦的 github-api jenkins-groovy
