Skip to content

Instantly share code, notes, and snippets.

@tusharvikky
Created March 18, 2022 20:34
Show Gist options
  • Save tusharvikky/413a800eb29601fd2947cc160c2f061e to your computer and use it in GitHub Desktop.
Save tusharvikky/413a800eb29601fd2947cc160c2f061e to your computer and use it in GitHub Desktop.
Setup CD using ECR and ECS
version: 0.2
phases:
install:
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
- timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <aws-account-id>.dkr.ecr.us-east-1.amazonaws.com
- PROJECT_NAME=<project-name>-staging
- REPOSITORY_URI=<aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/$PROJECT_NAME
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
- CONTAINER_NAME=<project-name>-container-staging
- DOCKERHUB_USERNAME=<dockerhub-username>
- DOCKERHUB_PASSWORD=<dockerhub-password>
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- echo $DOCKERHUB_PASSWORD | docker login --username $DOCKERHUB_USERNAME --password-stdin # environment set to aws secrets
- docker image build -t $PROJECT_NAME .
- docker image tag $PROJECT_NAME:latest $REPOSITORY_URI:latest
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- echo Writing image definitions file...
- printf '[{"name":"'"$CONTAINER_NAME"'","imageUri":"%s"}]' $REPOSITORY_URI:latest > imagedefinitions.json
- cat imagedefinitions.json
artifacts:
files: imagedefinitions.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment