Passing varible in Groovy script

18 Views Asked by At

I'm trying to pass a varible in a Groovy script which has been using in Jenkins pipelin but it fails.

...
def jsonResponse = readJSON text: uploadResponse
def uploadID = jsonResponse.message      
            
def response_analyze = sh(script: '''
    curl_output=$(     curl -X POST "http://10.10.10.10:8081/repo/api/v2/jobs" \
         -H "Authorization: Bearer xxxxxxxxxxxxxxx" \
         -H "folderId: 3" \
         -H "uploadId:${uploadID}" \
         -H "Content-Type: application/json" \
 }')

    echo \$curl_output
'''.trim(), returnStdout: true).trim()

// Parse the JSON response
def jsonResponseAnalyze = readJSON text: response_analyze
def message_analyze_ID = jsonResponseAnalyze.message
...

So I want to use "uploadID" variable in the curl command where "-H uploadID".

I tried with "-H "uploadId:${uploadID}" " and other stuff but still give "upload id should be integers" error.

How can I do that?

1

There are 1 best solutions below

0
Iterokun On BEST ANSWER

Use triple double quotes:

def response_analyze = sh(script: """
    curl_output=\$(     curl -X POST "http://10.10.10.10:8081/repo/api/v2/jobs" \
         -H "Authorization: Bearer xxxxxxxxxxxxxxx" \
         -H "folderId: 3" \
         -H "uploadId:${uploadID}" \
         -H "Content-Type: application/json" \
 }')

    echo \$curl_output
""".trim(), returnStdout: true).trim()

Don't forget to escape $ that you want to be expanded in shell, not in groovy.