无法使AWS CLI命令在Jenkins脚本中运行

问题描述

我正在运行一个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"

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

解决方法

好像是引号问题,您可以在管道下面尝试。

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