首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Groovy in jenkins - curl to Github Api

Groovy in jenkins - curl to Github Api
EN

Stack Overflow用户
提问于 2022-06-09 11:34:55
回答 1查看 226关注 0票数 0

尝试从Groovy向GitHub API发送请求:

代码语言:javascript
运行
复制
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}")

它显示它执行以下操作:

代码语言:javascript
运行
复制
curl -X PUT -H "Authorization: token ****" -d "{\"labels\":[\"my-label\"]}" https://api.github.com/repos/my-user/my-repo/issues/1/labels

当我在本地运行此命令(包括所有转义字符)时,它完全工作正常。

但是詹金斯-这个回来了

代码语言:javascript
运行
复制
curl: (6) Could not resolve host: token

curl: (6) Could not resolve host: ****"

curl: (3) unmatched close brace/bracket in URL position:

my-label\"]}"

          ^

以及:

代码语言:javascript
运行
复制
{

"message": "Not Found",

"documentation_url": "https://docs.github.com/rest/reference/issues#set-labels-for-an-issue"

}

因此,头转义似乎不起作用-我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-10 02:30:13

试试下面这样的东西。

代码语言:javascript
运行
复制
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"
                    }
            }
        }
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72559567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档