【问题标题】:Error in in codebuild while executing if else condition inside for loop在 for 循环中执行 if else 条件时代码构建中的错误
【发布时间】:2020-10-21 07:05:03
【问题描述】:

下面是我的 buildspec.yaml 文件,用于列出 ecr 中的图像,我正在使用 to for 循环以及查找特定 imageId 的 elese 条件是否存在

version: 0.2

phases:

  install:
    runtime-versions:
      nodejs: 12
    commands:
      - echo test
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)

  build:
    commands:
      - aws ecr list-images --repository-name mytestecrrepo > ecr.json
      - cat ecr.json
      - for imageTag in $(jq -r '.imageIds[].imageTag' < ecr.json); do 
        if [ $imageTag = "1.0" ]; then 
        echo "value exists" 
        else 
        echo "value doesntexists" 
        fi 
        done
  post_build:
    commands:
      - echo Build completed on `date`

由于某种原因,代码构建抛出以下错误

/codebuild/output/tmp/script.sh:第 9 行:语法错误:文件意外结束

我在 shell 脚本中做错了什么?

【问题讨论】:

    标签: bash amazon-web-services shell jq aws-codebuild


    【解决方案1】:

    您可以在build 中尝试以下操作:

      build:
        commands:
          - aws ecr list-images --repository-name mytestecrrepo > ecr.json
          - cat ecr.json
          - |
            for imageTag in $(jq -r '.imageIds[].imageTag' < ecr.json); do 
            if [ $imageTag = "1.0" ]; then 
            echo "value exists" 
            else 
            echo "value doesntexists" 
            fi 
            done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2019-12-04
      • 2018-07-08
      相关资源
      最近更新 更多