Skip to content

Commit ccd3d92

Browse files
McNaBrysamuelim01
andauthored
Create GitHub Actions for auto deployment to AWS (#66)
* Create github workflow to deploy frontend and backend to AWS * Add conditional trigger for backend workflow * The build is triggered for a specific service only if its files have been changes * Update frontend workflow * Remove trigger on pull request * Add conditional trigger based on frontend directory * Add cloudfront cache invalidation to frontend workflow Co-authored-by: samuelim01 <[email protected]> * Include collab in backend workflow * Add workflow scripts that can only be triggered manually for frontend and backend * Rename frontend force workflow * Change frontend and backend workflows to trigger only on production branch --------- Co-authored-by: samuelim01 <[email protected]> Co-authored-by: Samuel Lim <[email protected]>
1 parent dea4987 commit ccd3d92

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

.github/workflows/backend.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Deploy Backend Services
2+
3+
on:
4+
push:
5+
branches: [ 'production' ]
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
id-token: write # This is required for requesting the JWT
11+
contents: read # This is required for actions/checkout
12+
13+
env:
14+
AWS_REGION: ap-southeast-1
15+
ECS_CLUSTER: backend-cluster
16+
17+
jobs:
18+
deploy:
19+
name: Deploy Backend Service
20+
runs-on: ubuntu-latest
21+
environment: production
22+
23+
strategy:
24+
matrix:
25+
service: [ 'question', 'user', 'match', 'collaboration' ]
26+
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v4
30+
31+
- name: Configure AWS credentials
32+
id: aws-configure
33+
uses: aws-actions/[email protected]
34+
with:
35+
role-to-assume: ${{ secrets.AWS_BACKEND_ROLE }}
36+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
37+
aws-region: ${{ env.AWS_REGION }}
38+
39+
- name: Login to AWS ECR
40+
id: login-ecr
41+
uses: aws-actions/[email protected]
42+
43+
- name: Build and push ${{ matrix.service }} image to AWS ECR
44+
id: build-image
45+
env:
46+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
47+
ECR_REPOSITORY: ${{ matrix.service }}
48+
IMAGE_TAG: latest
49+
run: |
50+
echo "Building $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
51+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./services/${{ matrix.service }}
52+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
53+
54+
- name: Update AWS Service (${{ matrix.service }}) # Trigger re-deployment with latest image
55+
id: update-service
56+
env:
57+
ECS_SERVICE: ${{ matrix.service }}-service
58+
run: |
59+
echo "Updating $ECS_SERVICE for $ECS_CLUSTER"
60+
aws ecs update-service \
61+
--cluster $ECS_CLUSTER \
62+
--service $ECS_SERVICE \
63+
--force-new-deployment \
64+
--region $AWS_REGION

.github/workflows/frontend.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Deploy Frontend
2+
3+
on:
4+
push:
5+
branches: [ 'production' ]
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
id-token: write # This is required for requesting the JWT
11+
contents: read # This is required for actions/checkout
12+
13+
env:
14+
AWS_REGION: ap-southeast-1
15+
S3_BUCKET_NAME: app.peerprep.org
16+
17+
jobs:
18+
deploy:
19+
name: Deploy Frontend
20+
runs-on: ubuntu-latest
21+
environment: production
22+
23+
steps:
24+
- uses: actions/checkout@v4
25+
26+
- name: Configure AWS credentials
27+
id: aws-configure
28+
uses: aws-actions/[email protected]
29+
with:
30+
role-to-assume: ${{ secrets.AWS_FRONTEND_ROLE }}
31+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
32+
aws-region: ${{ env.AWS_REGION }}
33+
34+
- name: Build frontend distribution
35+
working-directory: frontend
36+
run: npm ci && npm run build
37+
38+
- name: Sync distribution to S3
39+
run: |
40+
aws s3 sync ./frontend/dist/frontend/browser/ s3://$S3_BUCKET_NAME --delete
41+
42+
- name: Invalidate Cloudfront Cache
43+
run: |
44+
aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_ID }} --paths "/*"

0 commit comments

Comments
 (0)