kevin
kevin

Reputation: 3488

How can I use env variables in this buildspec.yml file?

I've been building out this buildspec.yml file to configure my CodeBuild integration and while I can inject variables everywhere else in the script, it fails when I try to repeat the pattern in the printf section shown below. I'm out of ideas in regards to setting it up and I'm curious if anyone can point me in the right direction.

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{"name":"<HOW_TO_PLACE_VAR_HERE>","imageUri":"<HOW_TO_PLACE_VAR_HERE>"}]' $IMAGE_REPO_NAME:$IMAGE_TAG > imagedefinitions.json

       ^ how would I inject variables here? the $PATTERN doesn't work.
artifacts:
  files:
    - '**/*'
    - 'imagedefinitions.json'
  name: s3-to-s3-latest-build.zip
  discard-paths: no

Upvotes: 0

Views: 1036

Answers (1)

mtk
mtk

Reputation: 13709

printf '[{"name":"<HOW_TO_PLACE_VAR_HERE>","imageUri":"<HOW_TO_PLACE_VAR_HERE>"}]' $IMAGE_REPO_NAME:$IMAGE_TAG > imagedefinitions.json

Firstly, you are sending the output to a file, so probably you have not checked the actual contents.

Secondly, you can go with echo statement as you have used elsewhere

echo '[{"name":"'$IMAGE_REPO_NAME'"}]'

you can still send the contents to a file by redirecting the stdout to required file

echo '[{"name":"'$IMAGE_REPO_NAME'"}]' > imagedefinitions.json

Upvotes: 1

Related Questions