简体   繁体   中英

How to run complex curl command (with json) from groovy file on windows slave

I have the below CURL (for bitbucket tagging) command which include json content ( --data ). It is working from my CMD on Windows (of course I replaced sensitive info with dummy string):

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"

Now I'm trying to run this command on jenkinsfile.groovy file which runs on windows slave. This is the current command after some tries and the latest exception:

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

error:

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

can you suggest what is wrong? It's confusing as it's mix of json , groovy and windows

UPDATE

Following the below suggested and working solution, this is the final structure of 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM