Skip to content

Commit bdb8ece

Browse files
authored
Merge pull request #13 from davidsilva/feature/terraform-aws-cd
Feature/terraform aws cd
2 parents dcce5c5 + 25a81c6 commit bdb8ece

File tree

87 files changed

+5735
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+5735
-236
lines changed

.github/workflows/ci.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ on:
77
- development
88
- stage
99
- production
10+
- workflow-test
1011
pull_request:
1112
branches:
1213
- main
1314
- development
1415
- stage
1516
- production
17+
- workflow-test
18+
19+
env:
20+
AWS_REGION: us-east-1
21+
22+
permissions:
23+
id-token: write # This is required for requesting the JWT
24+
contents: read # This is required for actions/checkout
1625

1726
jobs:
1827
test:
@@ -63,6 +72,8 @@ jobs:
6372

6473
- name: Run backend tests
6574
working-directory: ./backend
75+
env:
76+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
6677
run: npm test
6778

6879
- name: Install dependencies for frontend
@@ -73,3 +84,95 @@ jobs:
7384
working-directory: ./frontend
7485
run: npm test
7586

87+
deploy:
88+
runs-on: ubuntu-latest
89+
needs: test
90+
91+
steps:
92+
- name: Checkout code
93+
uses: actions/checkout@v2
94+
95+
- name: Set up Node.js
96+
uses: actions/setup-node@v2
97+
with:
98+
node-version: '22'
99+
100+
- name: Configure AWS credentials
101+
uses: aws-actions/configure-aws-credentials@v1
102+
with:
103+
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/development-github-actions-role
104+
aws-region: ${{ env.AWS_REGION }}
105+
106+
- name: Install dependencies for backend
107+
working-directory: ./backend
108+
run: npm install
109+
110+
- name: Package migration files
111+
working-directory: ./backend
112+
run: ./package-migrations.sh
113+
114+
- name: Update Lambda migration function
115+
run: |
116+
aws lambda update-function-code \
117+
--function-name development-interview-prep-migrate \
118+
--zip-file fileb://backend/migrate-lambda/migrate-package.zip \
119+
--region ${{ env.AWS_REGION }}
120+
121+
- name: Wait for Lambda update
122+
run: |
123+
max_attempts=100
124+
attempt=0
125+
while true; do
126+
status=$(aws lambda get-function-configuration --function-name development-interview-prep-migrate --region ${{ env.AWS_REGION }} --query 'LastUpdateStatus' --output text)
127+
if [ "$status" == "Successful" ]; then
128+
break
129+
fi
130+
if [ "$status" == "Failed" ]; then
131+
echo "Lambda update failed"
132+
exit 1
133+
fi
134+
if [ $attempt -ge $max_attempts ]; then
135+
echo "Lambda update timed out"
136+
exit 1
137+
fi
138+
attempt=$((attempt + 1))
139+
echo "Waiting for Lambda update to complete..."
140+
sleep 5
141+
done
142+
143+
- name: Invoke Lambda function for migrations
144+
run: |
145+
aws lambda invoke \
146+
--function-name development-interview-prep-migrate \
147+
--region ${{ env.AWS_REGION }} \
148+
outputfile.txt
149+
env:
150+
NODE_ENV: development
151+
DATABASE_URL: postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_NAME }}
152+
153+
- name: Set up Docker Buildx
154+
uses: docker/setup-buildx-action@v1
155+
156+
- name: Log in to Amazon ECR
157+
id: login-ecr
158+
uses: aws-actions/amazon-ecr-login@v1
159+
160+
- name: Build and push backend Docker image
161+
working-directory: ./backend
162+
run: |
163+
docker build -t ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-backend:latest -f Dockerfile .
164+
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-backend:latest
165+
166+
- name: Build and push frontend Docker image
167+
working-directory: .
168+
run: |
169+
docker build -t ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-frontend:latest -f Dockerfile .
170+
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com/interview-prep-frontend:latest
171+
172+
- name: Update ECS service for backend
173+
run: |
174+
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.BACKEND_SERVICE_NAME }} --force-new-deployment
175+
176+
- name: Update ECS service for frontend
177+
run: |
178+
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.FRONTEND_SERVICE_NAME }} --force-new-deployment

.terraform.lock.hcl

Lines changed: 0 additions & 25 deletions
This file was deleted.

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Dockerfile for production-like deployments
2+
# Stage 1: Build the Angular application
3+
FROM node:20-bookworm AS build
4+
5+
WORKDIR /app/frontend
6+
7+
COPY frontend/package.json ./
8+
9+
RUN npm install
10+
11+
COPY frontend /app/frontend
12+
13+
RUN npm install -g @angular/cli
14+
15+
RUN npm run build:dev
16+
17+
# Stage 2: Prepare to serve the Angular application with Nginx
18+
FROM nginx:alpine AS nginx
19+
20+
COPY --from=build /app/frontend/dist/interview-prep/browser /usr/share/nginx/html
21+
COPY frontend/nginx.conf /etc/nginx/conf.d/default.conf
22+
23+
EXPOSE 80

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ Prerequisites:
112112
1. Specify this repo and choose the Development branch.
113113
1. Create `.env.local` and `.env.test` files based on `.env.example` and customize the values as necessary.
114114
1. Open a terminal and run `docker-compose --env-file .env.local build`.
115-
1. Run migrations to set up the database tables: `docker-compose --env-file .env.local up migrate`
115+
1. `export NODE_ENV=local`
116+
1. Run migrations to set up the database tables: `docker-compose --env-file .env.local -f docker-compose.yml -f docker-compose.migrate.yml up`
116117

117118
## Run the App
118119

@@ -121,6 +122,18 @@ Prerequisites:
121122

122123
## Running Tests
123124

125+
To run both frontend and backend tests:
126+
127+
1. `docker-compose --env-file .env.test -f docker-compose.test.yml up`
128+
129+
To run just frontend tests:
130+
131+
1. `docker-compose --env-file .env.test -f docker-compose.test.yml up frontend-tests`
132+
133+
To run just backend tests:
134+
135+
1. `docker-compose --env-file .env.test -f docker-compose.test.yml up backend-tests`
136+
124137
### Backend
125138

126139
1. `docker-compose --env-file .env.test up backend-tests`
@@ -138,7 +151,7 @@ I thought it would be nice to have a *live* web display of test results and cove
138151

139152
### Frontend
140153

141-
1. `docker-compose --env-file .env.test up frontend-tests` (At the moment I don't have any pretty web reports set up.)
154+
1. `docker-compose --env-file .env.test -f docker-compose.test.yml up frontend-tests` (At the moment I don't have any pretty web reports set up.)
142155

143156
Tests can also be run in watch mode so that they will be re-run as you make changes to code and tests:
144157

backend/Dockerfile

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
FROM node:20
1+
# Stage 1: Build the Node.js application
2+
FROM node:20 AS build
23

34
WORKDIR /app
45

5-
COPY backend/package.json ./
6+
COPY package.json ./
67

78
RUN npm install
89

9-
COPY backend /app/backend
10+
COPY . .
1011

11-
WORKDIR /app/backend
12+
RUN npm run build
13+
14+
# Stage 2: Prepare to serve the application
15+
FROM node:20-alpine
16+
17+
WORKDIR /app
18+
19+
COPY --from=build /app/dist /app/dist
20+
COPY --from=build /app/package.json /app/package.json
21+
22+
# Exclude devDependencies
23+
RUN npm install --only=production
24+
25+
ENV DEBUG=* NPM_CONFIG_LOGLEVEL=verbose
1226

1327
EXPOSE 3000
1428

backend/migrate-lambda/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src/migrations/
2+
dist
3+
migrate-package.zip

0 commit comments

Comments
 (0)