Skip to content

Instantly share code, notes, and snippets.

@breiter
Last active June 16, 2023 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breiter/09ce1cc4e01531b63aa91f91d8114eeb to your computer and use it in GitHub Desktop.
Save breiter/09ce1cc4e01531b63aa91f91d8114eeb to your computer and use it in GitHub Desktop.
ECS Deploymnet GitHub Action template
# Replace <items-in-angle-brackets> to cofigure
name: Build <stage|production> container and deploy
on:
push:
branches: [ <stage|production> ]
env:
ECR_REPOSITORY: <repo-name>
AWS_REGION: us-east-1
ECS_CLUSTER: <custer-name>
ECS_SERVICE: <service-name>
ECS_TASK: <task-name>
CONTAINER_NAME: <container-name>
BUILD_TAG: <stage|prod>
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 'Checkout repository with submodules'
uses: actions/checkout@v2
with:
submodules: recursive
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build --pull --no-cache -t ${{ env.CONTAINER_NAME }}:latest .
docker tag ${{ env.CONTAINER_NAME }}:latest ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
docker tag ${{ env.CONTAINER_NAME }}:latest ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.BUILD_TAG }}
docker tag ${{ env.CONTAINER_NAME }}:latest ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.BUILD_TAG }}-${{ github.sha }}
docker tag ${{ env.CONTAINER_NAME }}:latest ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
docker push ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
docker push ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.BUILD_TAG }}
docker push ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.BUILD_TAG }}-${{ github.sha }}
docker push ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK }} --query taskDefinition > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
force-new-deployment: true
wait-for-service-stability: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment