无法在 Jenkins 脚本中将 AWS CLI 命令获取到 运行

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

我正在 运行 执行 AWS 命令​​并尝试捕获命令

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
        )

但我一直收到错误消息:

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"

但是如果我在终端中 运行 命令,它工作正常:

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"

它处理报价的方式似乎有问题,但我不确定如何解决。我尝试用 "\""/x22 替换 ResourceRecordSets 周围的双引号,但它仍然会引发错误。

好像是引号问题,你可以试试下面的管道。

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"
                '''
            }
        }
    }
}

输出:

[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