【问题标题】:"Expecting property name enclosed in double quotes" with AWS cli on Jenkins Pipeline使用 Jenkins 管道上的 AWS cli “期望用双引号括起来的属性名称”
【发布时间】:2017-09-27 09:42:48
【问题描述】:

我收到一条错误消息

期望用双引号括起来的属性名称:第 1 行第 3 列(字符 2)

在 Jenkins 管道上运行 AWS CLI 以创建 EC2 容器服务的任务定义。有趣的是这个脚本能够在命令行中运行而没有任何错误。

aws ecs register-task-definition --family ${FAMILY} --container-definitions "[{\"name\":\"wildfly\",\"image\":\"${REPOSITORY}\",\"memory\":3024,\"essential\":true,\"portMappings\":[{\"containerPort\":8080,\"hostPort\":8080,\"protocol\":\"tcp\"}]}]"` 

这是完整的错误信息

aws ecs register-task-definition --family wildfly2-b47 --container-definitions [{name:wildfly, image:****/backend:b47, memory:3024, essential:true, portMappings:[{containerPort:8080, hostPort:8080, protocol:tcp}]}]

期望用双引号括起来的属性名称:第 1 行第 3 列 (字符 2)

这是詹金斯阶段代码

    stage('Deploy')
        withCredentials([string(credentialsId: 'ecr-repository', variable: 'repo'), 
        [$class: 'AmazonWebServicesCredentialsBinding', 
        accessKeyVariable: 'AWS_ACCESS_KEY_ID', 
        credentialsId: 'ecr-credentials', 
        secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
            sh '''
            bash
            set -x
            export AWS_DEFAULT_REGION=us-west-2
            CLUSTER="default"
            VERSION=b${BUILD_NUMBER}
            FAMILY=nxpmp2-${VERSION}
            SERVICE="backend"
            REPOSITORY=${repo}/backend:${VERSION}
            #Register the task definition in the repository
            aws ecs register-task-definition --family ${FAMILY} --container-definitions "[{"name": "wildfly", "image": ${REPOSITORY}, "memory": 3024, "essential": true, "portMappings": [{"containerPort": 8080, "hostPort": 8080, "protocol": "tcp" } ] }]"
            #Update the Service
            #aws ecs update-service --cluster ${CLUSTER} --region ${AWS_DEFAULT_REGION} --service ${SERVICE} --task-definition ${FAMILY}
--desired-count 1
            '''
        }

请帮帮我

【问题讨论】:

    标签: amazon-web-services jenkins amazon-ec2 jenkins-pipeline amazon-ecs


    【解决方案1】:

    查看--container-definitions 部分。您已经通过语法高亮看到它:

    --container-definitions "[{"name":
    

    您在双引号内使用双引号(name 附近)。

    以下应该可以代替:

    --container-definitions '[{"name": ...}]'
    

    【讨论】:

      【解决方案2】:

      如果你想在管道的 shell 脚本中使用一些 groovy 变量,我上面的答案是有问题的。

      问题是 aws cli 坚持一个 quete,然后你不能将你的 froovy 变量指向如下:

      FOO = "bar"
      sh'''
         aws ecs ...  --container-definitions '[{"name": $FOO}]'
      '''
      

      这不会给你 bar 作为输出,但会给 $FOO 这将导致错误。 经过几个小时的折腾并未能找到干净的解决方案后,我在这篇文章中找到了解决方法 Access a Groovy variable from within shell step in Jenkins pipeline

      所以解决办法是

      FOO = "bar"
      sh'''
         aws ecs ...  --container-definitions '[{"name":''' + $FOO + '''}]'
      '''
      

      希望能帮到你

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 1970-01-01
        相关资源
        最近更新 更多