-
Notifications
You must be signed in to change notification settings - Fork 1
194 lines (165 loc) · 6.64 KB
/
staging.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# References https://docs.docker.com/build/ci/github-actions/
# https://stackoverflow.com/questions/77740410/github-docker-build-push-action-with-multiple-architectures-is-slow
# https://dlmade.medium.com/ci-cd-with-github-action-and-aws-eks-5fd9714010cd
name: 'Staging Environment CI/CD Pipeline'
on:
push:
branches:
- frontend-staging
- staging
pull_request:
branches:
- frontend-staging
- staging
workflow_dispatch:
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [20, 22]
name: Run Tests on ${{ matrix.os }} with Node ${{ matrix.node }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Codebase
uses: actions/checkout@v4
- name: Setup Node ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Install Frontend Node Dependencies
working-directory: peer-prep
run: npm ci
- name: Test Build on Frontend
working-directory: peer-prep
run: npm run build
build:
# build container for both x86 and arm platforms
needs: test
environment: staging
name: Build Staging Containers and Push to Docker Hub
runs-on: ubuntu-latest
strategy:
matrix:
arch: [amd64, arm64]
steps:
- name: Checkout Codebase
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build User Service Image
uses: docker/build-push-action@v6
with:
context: ./user-service
file: ./user-service/Dockerfile
push: true
platforms: linux/${{ matrix.arch }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-user-service:staging-${{ vars.DOCKER_IMAGE_VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-user-service:staging-${{ github.sha }}
- name: Build Question Service Image
uses: docker/build-push-action@v6
with:
context: ./question-service
file: ./question-service/Dockerfile
push: true
platforms: linux/${{ matrix.arch }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-question-service:staging-${{ vars.DOCKER_IMAGE_VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-question-service:staging-${{ github.sha }}
- name: Build Matching Service Image
uses: docker/build-push-action@v6
with:
context: ./matching-service
file: ./matching-service/Dockerfile
push: true
platforms: linux/${{ matrix.arch }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-matching-service:staging-${{ vars.DOCKER_IMAGE_VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-matching-service:staging-${{ github.sha }}
- name: Build Collaboration Service Image
uses: docker/build-push-action@v6
with:
context: ./collaboration-service
file: ./collaboration-service/Dockerfile
push: true
platforms: linux/${{ matrix.arch }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-collaboration-service:staging-${{ vars.DOCKER_IMAGE_VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/peerprep-collaboration-service:staging-${{ github.sha }}
deploy:
needs: build
environment: staging
name: Deploy Staging Environment
runs-on: ubuntu-latest
steps:
- name: Checkout Codebase
uses: actions/checkout@v4
- name: Setup Node 20
uses: actions/setup-node@v4
with:
node-version: 20
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# Frontend Deployment to S3
- name: Copy Frontend Secrets into Build Folder
working-directory: peer-prep
shell: bash
env:
FRONTEND_SECRETS: ${{ secrets.FRONTEND_SECRETS }}
run: 'echo "$FRONTEND_SECRETS" | base64 --decode > .env'
- name: Build and Deploy Frontend on S3
working-directory: peer-prep
run: |
npm i
npm run build
- name: Upload to S3
working-directory: peer-prep/dist
run: |
aws s3 sync . s3://peerprep
- name: Retrieve S3 Bucket URL
run: echo "http://peerprep.s3-website-ap-southeast-1.amazonaws.com/"
# Backend Deployment to EKS
- name: Get EKS kubectl Configs
run: aws eks update-kubeconfig --region ${{ secrets.AWS_REGION }} --name ${{ secrets.EKS_CLUSTER_NAME }}
# echo out string into file taken from
# https://stackoverflow.com/questions/59481933/how-can-i-extract-secrets-using-github-actions
- name: Create Secrets File
working-directory: kubernetes
shell: bash
env:
K8S_SECRETS: ${{ secrets.KUBERNETES_SECRETS }}
run: 'echo "$KUBERNETES_SECRETS" | base64 --decode > 1-Kubernetes-Secrets.yml'
# kubectl apply the YML files to do a deployment
- name: Apply Secrets YML File to EKS Cluster
working-directory: kubernetes
run: kubectl apply -f 1-Kubernetes-Secrets.yml
- name: Apply Services YML File to EKS Cluster
working-directory: kubernetes
run: kubectl apply -f 2-PeerPrep-Services.yml
# replaces the placeholder text with the actual image
- name: Run sed on Compute Infrastructure YML File
working-directory: kubernetes
run: sed -i 's/IMAGE_TAG/staging-${{ vars.DOCKER_IMAGE_VERSION }}/' 3-PeerPrep-Staging-Compute-Infrastructure.yml
- name: Apply Compute Infrastructure YML File
working-directory: kubernetes
run: kubectl apply -f 3-PeerPrep-Compute-Infrastructure.yml
- name: Apply Ingress YML File to EKS Cluster
working-directory: kubernetes
run: kubectl apply -f 4-PeerPrep-Ingress.yml
- name: Restart Deployment to Force Redeployment of Containers in EKS Cluster
working-directory: kubernetes
run: kubectl rollout restart deployment ${{ secrets.EKS_CLUSTER_NAME }}
# Deploy changes to API Gateway
- name: Deploy Changes to API Gateway v1 Stage
run: |
aws apigatewayv2 create-deployment --api-id ${{ secrets.AWS_API_GATEWAY_ID }} --stage-name staging