version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 18 # specify your Node.js version here
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 254679257563.dkr.ecr.ap-south-1.amazonaws.com
      - echo $DOCKER_HUB_PASSWORD | docker login --username $DOCKER_HUB_USERNAME --password-stdin
      - REPOSITORY_URI=254679257563.dkr.ecr.ap-south-1.amazonaws.com/prod/flexifunnels-api-nodejs
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - aws s3 cp s3://ff-environment/production/api.env .env
      - docker build -t prod/flexifunnels-api-nodejs .
      - docker tag prod/flexifunnels-api-nodejs:latest 254679257563.dkr.ecr.ap-south-1.amazonaws.com/prod/flexifunnels-api-nodejs:latest
  post_build:
    commands:
      - echo Build completed on `date`
      - docker push 254679257563.dkr.ecr.ap-south-1.amazonaws.com/prod/flexifunnels-api-nodejs:latest
      - echo Writing image definitions file..
      - printf '[{"name":"ff-api-nodejs-prod-container","imageUri":"%s"}]' $REPOSITORY_URI:latest > imagedefinitions.json
      - cat imagedefinitions.json
artifacts:
  files: imagedefinitions.json
