Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing varible in Groovy script

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I do that?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading