Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aws] chore: Deploy to aws #60

Merged
merged 22 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/api-contract-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
sleep 15

- name: Print application logs
run: docker-compose logs
run: docker compose logs

- name: Test connectivity
run: curl ${API_URL}
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/backend-deploy-aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Deploy Backend to AWS

on:
workflow_dispatch:

jobs:
build:
defaults:
run:
working-directory: ./backend

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
run: |
yarn docker:app:build
docker tag notes-app-be:latest ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-be:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-be:latest
deploy:
needs: build
runs-on: ec2-runner-be
steps:
- name: Delete old be container
run: docker rm -f notes-app-be
- name: Delete old be image
run: docker image rm -f ${{ secrets.DOCKERHUB_USERNAME }}notes-app-fe
- name: Prune docker system
run: docker system prune -f
- name: Pull image from docker hub
run: docker pull ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-be:latest
- name: Run docker container
run: docker run -e DATABASE_URL=${{ secrets.PROD_DATABASE_URL }} -e JWT_SECRET=${{ secrets.JWT_SECRET }} -d -p 5000:5000 --name notes-app-be ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-be:latest
14 changes: 0 additions & 14 deletions .github/workflows/backend-deploy.yml

This file was deleted.

39 changes: 0 additions & 39 deletions .github/workflows/backend-docker-push.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/backend-e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
sleep 15

- name: Print application logs
run: docker-compose logs
run: docker compose logs

- name: Test connectivity
run: curl ${API_URL}
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/frontend-deploy-aws.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Deploy Frontend to AWS

on:
workflow_dispatch:

permissions: write-all

jobs:
build:
defaults:
run:
working-directory: ./frontend

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
run: |
docker build --build-arg REACT_APP_API_BASE_URL=${{ secrets.REACT_APP_API_BASE_URL }} -t notes-app-fe .
docker tag notes-app-fe:latest ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-fe:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-fe:latest
deploy:
needs: build
runs-on: ec2-runner-fe
steps:
- name: Delete old fe container
run: docker rm -f notes-app-fe
- name: Prune docker system
run: docker system prune -f
- name: Pull image from docker hub
run: docker pull ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-fe:latest
- name: Run docker container
run: docker run -e REACT_APP_API_BASE_URL=${{ secrets.REACT_APP_API_BASE_URL }} -d -p 3000:3000 --name notes-app-fe ${{ secrets.DOCKERHUB_USERNAME }}/notes-app-fe:latest
14 changes: 0 additions & 14 deletions .github/workflows/frontend-deploy.yml

This file was deleted.

39 changes: 0 additions & 39 deletions .github/workflows/frontend-docker-push.yml

This file was deleted.

70 changes: 0 additions & 70 deletions .github/workflows/frontend-service-tests-deploy.yml

This file was deleted.

41 changes: 22 additions & 19 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
# Use an official Node.js runtime based on Alpine Linux as the base image
FROM node:20-alpine

# Install bash
RUN apk add --no-cache bash
FROM node:20-alpine as builder

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package.json yarn.lock ./
# Install dependencies required for the build
RUN apk add --no-cache bash openssl3

# Copy package.json, package-lock.json, and TypeScript configuration files
COPY package.json yarn.lock tsconfig.json ./

# Install the application dependencies
RUN yarn

# Required for prisma
RUN apk add openssl3
# Copy the application code and other necessary files into the container
COPY . ./
COPY wait-for-it.sh wait-for-it.sh

# Copy the TypeScript configuration files
COPY tsconfig.json ./
# Make the script executable and build the application
RUN chmod +x wait-for-it.sh && \
npx prisma generate && \
yarn build

# Copy the application code into the container
COPY . .
# Use a new, clean base image for the runtime
FROM node:20-alpine

COPY wait-for-it.sh wait-for-it.sh
WORKDIR /usr/src/app

# Make the script executable
RUN chmod +x wait-for-it.sh
# Install dependencies required for the build
RUN apk add --no-cache bash

# Primsa generate
RUN npx prisma generate
# Copy only the built artifacts and necessary files from the builder stage
COPY --from=builder /usr/src/app/ ./

# Build TypeScript code
RUN yarn build
# Copy the wait-for-it.sh script and make it executable
RUN chmod +x wait-for-it.sh

# Expose the port that the application will run on
EXPOSE 5000
Expand Down
6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"build": "tsc",
"docker:app:build": "docker build -t notes-app-be .",
"docker:app:up": "docker run -p 5001:5001 notes-app-be",
"docker:up": "docker-compose up -d",
"docker:db:up": "docker-compose up postgres -d",
"docker:down": "docker-compose down",
"docker:up": "docker compose up -d",
"docker:db:up": "docker compose up postgres -d",
"docker:down": "docker compose down",
"prisma": "prisma migrate dev --name init",
"seed": "ts-node prisma/seed.ts",
"lint": "eslint .",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ app.use('/', noteRoutes);
app.use('/', userRoutes);
app.use('/', loginRoutes);

app.get('/health', (req, res) => {
app.get('/api/health', (req, res) => {
res.json({ status: 'ok' });
});

Expand Down
17 changes: 17 additions & 0 deletions backend/src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ router.get('/api/users', authenticateToken, async (req, res) => {
}
});

router.get('/api/user', authenticateToken, async (req, res) => {
// get id from decoded jwt token
const id = req.user.userId;

try {
const user = await prisma.user.findFirst({
where: { id },
});
// Remove password from the response
delete user.password;
res.json(user);
} catch (error) {
console.log('error', error);
res.status(500).send({ error: 'Oops, something went wrong' });
}
});

router.post('/api/users', async (req, res) => {
const { email, password, username } = req.body;

Expand Down
Loading
Loading