Skip to content

Commit

Permalink
[aws] chore: Deploy to aws (#60)
Browse files Browse the repository at this point in the history
* [aws] chore: Deploy to aws=
* [aws] feat: User Profile
  • Loading branch information
helloitsdave authored Aug 19, 2024
1 parent 80ba39f commit be9fec1
Show file tree
Hide file tree
Showing 28 changed files with 395 additions and 210 deletions.
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

2 comments on commit be9fec1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

98.23%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   authenticateToken.ts100%100%100%
   hash.ts100%100%100%
   index.ts50%50%83.33%31–33
   prisma.ts100%100%100%
src/__mocks__
   prisma.ts100%100%100%
src/routes
   loginRoutes.ts100%100%100%
   noteRoutes.ts100%100%100%
   userRoutes.ts100%100%100%

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

96.95%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   App.tsx87.50%75%96%30–31, 40–41
   NoteApp.tsx90.48%90.91%97.06%83–87, 92–93
src/api
   apiService.ts83.33%87.50%86.67%100–105, 40, 40–45, 81–83, 99
src/components
   Header.tsx100%100%100%
   Login.tsx81.82%100%98.72%32, 32–33
   Note.tsx100%100%100%
   NoteForm.tsx100%100%100%
   NoteFormModal.tsx100%100%100%
   NoteGrid.tsx100%100%100%
   RegistrationForm.tsx88.24%100%97.26%68, 68–72
   RegistrationLink.tsx100%100%100%
   Spinner.tsx100%100%100%
   UserModal.tsx100%100%100%
src/helpers
   validatePassword.ts100%100%100%

Please sign in to comment.