在 for 循环内执行 if else 条件时代码构建出错

Error in in codebuild while executing if else condition inside for loop

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

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 脚本中做错了什么?

您可以在 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