Skip to content

Commit

Permalink
Updated workflow to deploy to ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
blyedev committed Jun 15, 2024
1 parent cb1b55d commit 7a8611f
Showing 1 changed file with 95 additions and 28 deletions.
123 changes: 95 additions & 28 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
permissions:
contents: read

env:
CLUSTER_NAME: calendar-app-v2

jobs:
build-backend:
name: Build backend and push to ECR
Expand All @@ -18,6 +21,9 @@ jobs:
id-token: write
contents: read

outputs:
image: ${{ steps.build-image.outputs.image }}

steps:
- name: Checkout repo
uses: actions/checkout@v4
Expand All @@ -34,13 +40,15 @@ jobs:
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push backend Docker image
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: calendar-app-backend
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./backend
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
build-frontend:
name: Build frontend and push to ECR
Expand All @@ -51,6 +59,9 @@ jobs:
id-token: write
contents: read

outputs:
image: ${{ steps.build-image.outputs.image }}

steps:
- name: Checkout repo
uses: actions/checkout@v4
Expand All @@ -66,39 +77,95 @@ jobs:
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push backend Docker image
- name: Build, tag, and push frontend Docker image
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: calendar-app-frontend
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./frontend
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
deploy-backend:
name: Deploy Backend to ECS
runs-on: ubuntu-latest
environment: deploy-backend
needs: build-backend

permissions:
id-token: write
contents: read

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}

- name: Download backend task definition
run: |
aws ecs describe-task-definition --task-definition calendar-app-backend --query taskDefinition > backend-task-def.json
- name: Render Amazon ECS backend task definition
id: render-backend
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: backend-task-def.json
container-name: backend
image: ${{ needs.build-backend.outputs.image }}

# deploy:
# needs: [build-backend, build-frontend]
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v2
#
# - name: Update ECS task definition
# id: task-def
# env:
# BACKEND_IMAGE: ${{ steps.login-ecr.outputs.registry }}/your-backend-repo:${{ github.sha }}
# FRONTEND_IMAGE: ${{ steps.login-ecr.outputs.registry }}/your-frontend-repo:${{ github.sha }}
# run: |
# sed -i "s|PLACEHOLDER_BACKEND_IMAGE|$BACKEND_IMAGE|g" ecs-task-def.json
# sed -i "s|PLACEHOLDER_FRONTEND_IMAGE|$FRONTEND_IMAGE|g" ecs-task-def.json
# aws ecs register-task-definition --cli-input-json file://ecs-task-def.json
#
# - name: Get new task definition revision
# id: new-task-def
# run: |
# NEW_TASK_DEF=$(aws ecs describe-task-definition --task-definition your-task-family)
# echo "::set-output name=task-def-arn::$(echo $NEW_TASK_DEF | jq -r '.taskDefinition.taskDefinitionArn')"
#
# - name: Update ECS service to use new task definition
# run: |
# aws ecs update-service --cluster your-cluster --service your-backend-service --task-definition ${{ steps.new-task-def.outputs.task-def-arn }}
# aws ecs update-service --cluster your-cluster --service your-frontend-service --task-definition ${{ steps.new-task-def.outputs.task-def-arn }}
- name: Deploy Amazon ECS backend task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-backend.outputs.task-definition }}
service: backend-service
cluster: ${{ env.CLUSTER_NAME }}
wait-for-service-stability: true

deploy-frontend:
name: Deploy Frontend to ECS
runs-on: ubuntu-latest
environment: deploy-frontend
needs: build-frontend

permissions:
id-token: write
contents: read

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}

- name: Download frontend task definition
run: |
aws ecs describe-task-definition --task-definition calendar-app-frontend --query taskDefinition > frontend-task-def.json
- name: Render Amazon ECS frontend task definition
id: render-frontend
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: frontend-task-def.json
container-name: frontend
image: ${{ needs.build-frontend.outputs.image }}

- name: Deploy Amazon ECS frontend task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-frontend.outputs.task-definition }}
service: frontend-service
cluster: ${{ env.CLUSTER_NAME }}
wait-for-service-stability: true

0 comments on commit 7a8611f

Please sign in to comment.