-
Notifications
You must be signed in to change notification settings - Fork 0
190 lines (159 loc) · 5.95 KB
/
ci.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
name: CI
on:
push:
branches:
- main
- development
- stage
- production
- workflow-test
pull_request:
branches:
- main
- development
- stage
- production
- workflow-test
env:
AWS_REGION: us-east-1
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
PGPASSFILE: /home/runner/.pgpass
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '22'
- name: Create .pgpass file
run: |
echo "localhost:5432:postgres:postgres:postgres" > ~/.pgpass &&
echo "localhost:5432:testdb:postgres:postgres:postgres" >> ~/.pgpass &&
chmod 0600 ~/.pgpass
- name: Create test database
run: |
sudo apt-get install -y postgresql-client &&
psql -h localhost -U postgres -c "CREATE DATABASE testdb;"
- name: Install dependencies for backend
working-directory: ./backend
run: npm install
# Run integration tests separately to avoid db conflicts
- name: Run backend integration tests for users
working-directory: ./backend
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
run: npm test -- src/__tests__/integration/users.integration.test.ts
- name: Run backend integration tests for products
working-directory: ./backend
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
run: npm test -- src/__tests__/integration/products.integration.test.ts
# Unit tests can be run together
- name: Run backend unit tests
working-directory: ./backend
run: npm test -- src/__tests__/unit
- name: Install dependencies for frontend
working-directory: ./frontend
run: npm install
- name: Run frontend tests
working-directory: ./frontend
run: npm test
deploy:
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '22'
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/development-github-actions-role
aws-region: ${{ env.AWS_REGION }}
- name: Install dependencies for backend
working-directory: ./backend
run: npm install
- name: Package migration files
working-directory: ./backend
run: ./package-migrations.sh
- name: Update Lambda migration function
run: |
aws lambda update-function-code \
--function-name development-interview-prep-migrate \
--zip-file fileb://backend/migrate-lambda/migrate-package.zip \
--region ${{ env.AWS_REGION }}
- name: Wait for Lambda update
run: |
max_attempts=100
attempt=0
while true; do
status=$(aws lambda get-function-configuration --function-name development-interview-prep-migrate --region ${{ env.AWS_REGION }} --query 'LastUpdateStatus' --output text)
if [ "$status" == "Successful" ]; then
break
fi
if [ "$status" == "Failed" ]; then
echo "Lambda update failed"
exit 1
fi
if [ $attempt -ge $max_attempts ]; then
echo "Lambda update timed out"
exit 1
fi
attempt=$((attempt + 1))
echo "Waiting for Lambda update to complete..."
sleep 5
done
- name: Invoke Lambda function for migrations
run: |
aws lambda invoke \
--function-name development-interview-prep-migrate \
--region ${{ env.AWS_REGION }} \
outputfile.txt
env:
NODE_ENV: development
# sets up Docker Buildx to enable advanced build features in the workflow.
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build and push backend Docker image
working-directory: ./backend
run: |
docker build -t ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-backend:latest -f Dockerfile .
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-backend:latest
- name: Build and push frontend Docker image
working-directory: .
run: |
docker build -t ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-frontend:latest -f Dockerfile .
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-frontend:latest
- name: Update ECS service for backend
run: |
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.BACKEND_SERVICE_NAME }} --force-new-deployment
- name: Update ECS service for frontend
run: |
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.FRONTEND_SERVICE_NAME }} --force-new-deployment