Skip to content

Commit 7793c74

Browse files
committed
Create github workflow to deploy frontend and backend to AWS
1 parent 89cb0bb commit 7793c74

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

.github/workflows/backend.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Deploy Backend Services
2+
3+
on:
4+
push:
5+
branches: [ 'main' ]
6+
paths: [ 'services/match/**', 'services/question/**', 'services/user/**' ]
7+
pull_request:
8+
branches: [ 'main' ]
9+
types: [ 'opened', 'reopened', 'synchronize']
10+
11+
# Allows you to run this workflow manually from the Actions tab
12+
workflow_dispatch:
13+
14+
permissions:
15+
id-token: write # This is required for requesting the JWT
16+
contents: read # This is required for actions/checkout
17+
18+
env:
19+
AWS_REGION: ap-southeast-1
20+
ECS_CLUSTER: backend-cluster
21+
22+
jobs:
23+
deploy:
24+
name: Deploy Backend Service
25+
runs-on: ubuntu-latest
26+
environment: production
27+
28+
strategy:
29+
matrix:
30+
service: [ 'match', 'question', 'user' ]
31+
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
36+
- name: Check for changes in ${{ matrix.service }} directory
37+
uses: dorny/paths-filter@v3
38+
id: changes
39+
with:
40+
filters: |
41+
service:
42+
- '.services/${{ matrix.service }}/**'
43+
44+
- name: Exit if no changes found
45+
if: steps.changes.output.service == 'false'
46+
run: exit 0
47+
48+
- name: Configure AWS credentials
49+
id: aws-configure
50+
uses: aws-actions/[email protected]
51+
with:
52+
role-to-assume: ${{ secrets.AWS_BACKEND_ROLE }}
53+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
54+
aws-region: ${{ env.AWS_REGION }}
55+
56+
- name: Login to AWS ECR
57+
id: login-ecr
58+
uses: aws-actions/[email protected]
59+
60+
- name: Build and push ${{ matrix.service }} image to Amazon ECR
61+
id: build-image
62+
env:
63+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
64+
ECR_REPOSITORY: ${{ matrix.service }}
65+
IMAGE_TAG: latest
66+
run: |
67+
echo "Building $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
68+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./services/${{ matrix.service }}
69+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
70+
71+
- name: Update AWS Service (${{ matrix.service }}) # Trigger re-deployment with latest image
72+
id: update-service
73+
env:
74+
ECS_SERVICE: ${{ matrix.service }}-service
75+
run: |
76+
echo "Updating $ECS_SERVICE for $ECS_CLUSTER"
77+
aws ecs update-service \
78+
--cluster $ECS_CLUSTER \
79+
--service $ECS_SERVICE \
80+
--force-new-deployment \
81+
--region $AWS_REGION

.github/workflows/frontend.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Deploy Frontend
2+
3+
on:
4+
push:
5+
branches: [ 'main' ]
6+
paths:
7+
# - 'frontend/**' # Only trigger if changes are made in the frontend directory
8+
pull_request:
9+
branches: [ 'main' ]
10+
types: [ 'opened', 'reopened', 'synchronize']
11+
12+
workflow_dispatch:
13+
14+
permissions:
15+
id-token: write # This is required for requesting the JWT
16+
contents: read # This is required for actions/checkout
17+
18+
env:
19+
AWS_REGION: ap-southeast-1
20+
S3_BUCKET_NAME: app.peerprep.org
21+
22+
jobs:
23+
deploy:
24+
name: Deploy Frontend
25+
runs-on: ubuntu-latest
26+
environment: production
27+
28+
steps:
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_FRONTEND_ROLE }}
36+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
37+
aws-region: ${{ env.AWS_REGION }}
38+
39+
- name: Build frontend distribution
40+
working-directory: frontend
41+
run: npm ci && npm run build
42+
43+
- name: Sync distribution to S3
44+
run: |
45+
aws s3 sync ./frontend/dist/frontend/browser/ s3://$S3_BUCKET_NAME --delete

0 commit comments

Comments
 (0)