chris
chris

Reputation: 344

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

Below is my buildspec.yaml file to list the images inside ecr and I'm using to for loop and if elese condition to find a particular imageId exists or not

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`

For some reason, code build is throwing the below error

/codebuild/output/tmp/script.sh: line 9: syntax error: unexpected end of file

I'm doing something wrong within the shell script?

Upvotes: 1

Views: 3134

Answers (1)

Marcin
Marcin

Reputation: 238229

You can try the following in your 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

Upvotes: 5

Related Questions