Jenkins中的Groovy- curl 到Github Api

zujrkrfu  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(153)

正在尝试从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

当我在本地运行此命令(包括所有转义字符)时,它可以完美地工作
但在Jenkins-这返回

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"

}

所以看起来标题转义不知何故不起作用-我错过了什么?

jbose2ul

jbose2ul1#

请尝试以下内容。

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"
                    }
            }
        }
    }
}
}

相关问题