如何从 windows slave 上的 groovy 文件 运行 复杂的 curl 命令(使用 json)
How to run complex curl command (with json) from groovy file on windows slave
我有以下 CURL
(用于 bitbucket
标记)命令,其中包含 json
内容 (--data
)。它在 Windows
上从我的 CMD
开始工作(当然我用虚拟字符串替换了敏感信息):
curl -L -k --location-trusted -X POST --user "TEST58:123@456!" https://git.devops.test/rest/api/1.0/projects/M800/repos/test/tags
--data "{\"name\": \"test\",\"startPoint\": \"1234ca34f3624fea0e0a2134f123da11ae01\",\"message\": \"test\"}"
-H "X-Atlassian-Token: no-check" -H "Content-Type: application/json"
现在我正在尝试 运行 在 jenkinsfile.groovy
文件上执行此命令,该文件 运行 在 windows
从站上。
这是经过一些尝试和最新异常后的当前命令:
def body = "{name: ${testTag}, startPoint: ${commitHash}, message: ${message}}"
withCredentials([usernamePassword(usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD', credentialsId: "TEST")]) {
bat(returnStdout: true, script: "curl -L -k --location-trusted -X POST --user \"${USERNAME}:${PASSWORD}\" \"https://git.devops.test/rest/api/1.0/projects/${projectKey}/repos/${repoName}/tags\" -d \"${body}\" -H \"X-Atlassian-Token: no-check\" -H \"Content-Type: application/json\"")
}
错误:
{"errors":[{"context":null,"message":"Unexpected character ('n' (code 110)): was expecting double-quote to start field name\n at [Source: com.atlassian.stash.internal.web.util.web.CountingServletInputStream@447c81dc; line: 1, column: 3]","exceptionName":"org.codehaus.jackson.JsonParseException"}]}
你能指出哪里出了问题吗?它令人困惑,因为它混合了 json
、groovy
和 windows
更新
按照下面的建议和工作解决方案,这是 jenkisfile.groovy
的最终结构:
import groovy.json.JsonOutput
node ("win") {
stage("tag") {
def testTag = "testTag"
def message = "test"
def stdout = bat(returnStdout: true, script: "git rev-parse HEAD").trim()
def commitHash = stdout.readLines().drop(1).join(" ")
def body = [
name: "${testTag}",
startPoint: "${commitHash}",
message: "${message}",
]
body = JsonOutput.toJson(body) // normal json
body = JsonOutput.toJson(body) // escaped json - escape all doublequotes
def url = "https://git.devops.test/rest/api/1.0/projects/${projectKey}/repos/${repoName}/tags"
withCredentials([usernamePassword(usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD', credentialsId: "TEST")]) {
def cmd = """curl -L -k --location-trusted -X POST --user "${USERNAME}:${PASSWORD}" "${url}" -H "accept: application/json" --data ${body} -H "Content-Type: application/json" -H "X-Atlassian-Token: no-check" """
bat(script:cmd, returnStdout:true)
}
}
}
def body = [
name: 'aaa',
startPoint: 123,
message: "test",
]
body = JsonOutput.toJson(body) // normal json
body = JsonOutput.toJson(body) // escaped json - escape all doublequotes
def url = "https://httpbin.org/post"
def cmd = """curl -L -k --location-trusted -X POST --user "TEST58:123@456!" "$url" -H "accept: application/json" --data $body -H "Content-Type: application/json" -H "X-Atlassian-Token: no-check" """
println cmd
bat(script:cmd, returnStdout:true)
我有以下 CURL
(用于 bitbucket
标记)命令,其中包含 json
内容 (--data
)。它在 Windows
上从我的 CMD
开始工作(当然我用虚拟字符串替换了敏感信息):
curl -L -k --location-trusted -X POST --user "TEST58:123@456!" https://git.devops.test/rest/api/1.0/projects/M800/repos/test/tags
--data "{\"name\": \"test\",\"startPoint\": \"1234ca34f3624fea0e0a2134f123da11ae01\",\"message\": \"test\"}"
-H "X-Atlassian-Token: no-check" -H "Content-Type: application/json"
现在我正在尝试 运行 在 jenkinsfile.groovy
文件上执行此命令,该文件 运行 在 windows
从站上。
这是经过一些尝试和最新异常后的当前命令:
def body = "{name: ${testTag}, startPoint: ${commitHash}, message: ${message}}"
withCredentials([usernamePassword(usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD', credentialsId: "TEST")]) {
bat(returnStdout: true, script: "curl -L -k --location-trusted -X POST --user \"${USERNAME}:${PASSWORD}\" \"https://git.devops.test/rest/api/1.0/projects/${projectKey}/repos/${repoName}/tags\" -d \"${body}\" -H \"X-Atlassian-Token: no-check\" -H \"Content-Type: application/json\"")
}
错误:
{"errors":[{"context":null,"message":"Unexpected character ('n' (code 110)): was expecting double-quote to start field name\n at [Source: com.atlassian.stash.internal.web.util.web.CountingServletInputStream@447c81dc; line: 1, column: 3]","exceptionName":"org.codehaus.jackson.JsonParseException"}]}
你能指出哪里出了问题吗?它令人困惑,因为它混合了 json
、groovy
和 windows
更新
按照下面的建议和工作解决方案,这是 jenkisfile.groovy
的最终结构:
import groovy.json.JsonOutput
node ("win") {
stage("tag") {
def testTag = "testTag"
def message = "test"
def stdout = bat(returnStdout: true, script: "git rev-parse HEAD").trim()
def commitHash = stdout.readLines().drop(1).join(" ")
def body = [
name: "${testTag}",
startPoint: "${commitHash}",
message: "${message}",
]
body = JsonOutput.toJson(body) // normal json
body = JsonOutput.toJson(body) // escaped json - escape all doublequotes
def url = "https://git.devops.test/rest/api/1.0/projects/${projectKey}/repos/${repoName}/tags"
withCredentials([usernamePassword(usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD', credentialsId: "TEST")]) {
def cmd = """curl -L -k --location-trusted -X POST --user "${USERNAME}:${PASSWORD}" "${url}" -H "accept: application/json" --data ${body} -H "Content-Type: application/json" -H "X-Atlassian-Token: no-check" """
bat(script:cmd, returnStdout:true)
}
}
}
def body = [
name: 'aaa',
startPoint: 123,
message: "test",
]
body = JsonOutput.toJson(body) // normal json
body = JsonOutput.toJson(body) // escaped json - escape all doublequotes
def url = "https://httpbin.org/post"
def cmd = """curl -L -k --location-trusted -X POST --user "TEST58:123@456!" "$url" -H "accept: application/json" --data $body -H "Content-Type: application/json" -H "X-Atlassian-Token: no-check" """
println cmd
bat(script:cmd, returnStdout:true)