Search code examples
jenkinsshaws-clijmespath

Can't get AWS CLI Command to run in Jenkins script


I am running a AWS command and attempting to capture the command

weight = sh (
            script: """
            aws route53 list-resource-record-sets \
            --hosted-zone-id ${HOSTED_ZONE_ID} \
            --query "ResourceRecordSets[?Type == 'A' && Name == ${RECORD_NAME} && SetIdentifier == ${IDENTIFIER}].Weight"
            """,
            returnStdout: true
        )

but I keep getting the error:

aws route53 list-resource-record-sets --hosted-zone-id ABCABCABCABC --query 'ResourceRecordSets[?Type == '\''A'\'' && Name == test-e1-qa.aws.com. && SetIdentifier == test-qa-e1].Weight'
Bad value for --query ResourceRecordSets[?Type: Invalid jmespath expression: Incomplete expression:
"ResourceRecordSets[?Type"

But if I run the command in terminal, it works fine:

aws route53 list-resource-record-sets --hosted-zone-id ABCABCABCABC --query "ResourceRecordSets[?Type == 'A' && Name == 'test-e1-qa.aws.com.' && SetIdentifier == 'test-qa-e1'].Weight"

It looks like something is wrong with how it is processing quotes, but I am not sure how to fix it. I tried using replacing the double quotes surround the ResourceRecordSets with "\"" or /x22, but it still throws an error.


Solution

  • Seems like the quotes issue, you can try below pipeline.

    pipeline {
        agent none
        stages {
            stage ('getweight') {
                agent any
    
                steps {
                    sh '''#!/bin/bash
                    HOSTED_ZONE_ID=12345abcd
                    RECORD_NAME="'test.example.com.'"
                    aws route53 list-resource-record-sets --hosted-zone-id ${HOSTED_ZONE_ID} --query "ResourceRecordSets[?Type == 'A' && Name == ${RECORD_NAME} && SetIdentifier == 'test'].Weight"
                    '''
                }
            }
        }
    }
    

    output:

    [Pipeline] stage
    [Pipeline] { (getweight)
    [Pipeline] node
    Running on Jenkins in test
    [Pipeline] {
    [Pipeline] sh
    [testpipline] Running shell script
    [
        200
    ]
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] End of Pipeline
    Finished: SUCCESS