Last active
December 29, 2023 13:37
-
-
Save adshrc/fd20366eedfc474ee974cc784b6ad7f5 to your computer and use it in GitHub Desktop.
buildspec.yml: Build Docker Image, push to ECR and deploy to ECS (imagedefinitions.json)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 0.2 | |
# Set the following env vars in CodeBuild (Environment Settings) | |
# You will get most of them from the ECR URI: <ACCOUNT_ID>.dkr.ecr.<ECR_REGION>.amazonaws.com/<ECR_REPOSITORY_NAME> | |
# | |
# ACCOUNT_ID | |
# ECR_REGION | |
# ECR_REPOSITORY_NAME | |
# ECS_CONTAINER_NAME | |
# | |
# The CodeBuild ServiceRole needs to have the following Permissions to upload to your (private) ECR | |
# | |
# "ecr:BatchCheckLayerAvailability", | |
# "ecr:CompleteLayerUpload", | |
# "ecr:GetAuthorizationToken", | |
# "ecr:InitiateLayerUpload", | |
# "ecr:PutImage", | |
# "ecr:UploadLayerPart" | |
phases: | |
pre_build: | |
commands: | |
- ECR_REPOSITORY_BASE_URI=$ACCOUNT_ID.dkr.ecr.$ECR_REGION.amazonaws.com | |
- ECR_REPOSITORY_URI=$ECR_REPOSITORY_BASE_URI/$ECR_REPOSITORY_NAME | |
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) # we need the short hash | |
- IMAGE_TAG=${COMMIT_HASH:=latest} # use short hash or fallback to latest | |
- echo Logging in to Amazon ECR... | |
- aws ecr get-login-password --region $ECR_REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY_BASE_URI | |
build: | |
commands: | |
- echo Building the Docker image... | |
- docker build -t $ECR_REPOSITORY_URI:latest . | |
- docker tag $ECR_REPOSITORY_URI:latest $ECR_REPOSITORY_URI:$IMAGE_TAG | |
post_build: | |
commands: | |
- echo Pushing the Docker images... | |
- docker push $ECR_REPOSITORY_URI:latest | |
- docker push $ECR_REPOSITORY_URI:$IMAGE_TAG | |
- echo Writing image definitions file... | |
- printf '[{"name":"%s","imageUri":"%s"}]' "$ECS_CONTAINER_NAME" "$ECR_REPOSITORY_URI:$IMAGE_TAG" > imagedefinitions.json | |
artifacts: | |
files: imagedefinitions.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment