amazon-web-servicesamazon-s3aws-codepipelineaws-codebuildbuildspec

Parameterize s3 bucket name in buildspec.yml file


Right now I hardcoded the bucket name to cp report file to that bucket but I want to create an environment variable which can pull a specific bucket from s3 bucket list and then cp my report file to the same bucket. i have read many articles but non helped me. can someone help with this?

Hardcoded buildspec.yml file

version: 0.2

phases:
  pre_build:
    commands:
      - docker-compose up -d
  build:
    commands:
      - mvn clean verify

  post_build:
    commands:
      - ls -lrt target
      - aws s3 cp target/site/report/Summary.html s3://test-reports-s3
      - docker-compose down

New buildspec.yml file i tried

version: 0.2

env:
  variables:
    S3_BUCKET: aws s3 ls | grep "my-bucket-identifier"

phases:
  pre_build:
    commands:
      - docker-compose up -d
  build:
    commands:
      - mvn clean verify

  post_build:
    commands:
      - ls -lrt target
      - aws s3 cp target/site/report/Summary.html $S3_BUCKET
      - docker-compose down

Note: I am parameterizing this because we cannot create same name of the S3 bucket in different aws account. As S3 Bucket names must be globally Unique. i have different environments example QA, Demo and Stage each will have different bucket names


Solution

  • You can't have commands in variables. But you can do that in other sections,e.g.:

      post_build:
        commands:
          - ls -lrt target
          - |
            S3_BUCKET=`aws s3 ls | grep "my-bucket-identifier"`
            aws s3 cp target/site/report/Summary.html $S3_BUCKET
          - docker-compose down