尝试从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"
}
因此,头转义似乎不起作用-我遗漏了什么?
发布于 2022-06-10 02:30:13
试试下面这样的东西。
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://stackoverflow.com/questions/72559567
复制相似问题