Skip to content

Commit

Permalink
Merge pull request #374 from bounswe/feature/cont-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr authored Oct 15, 2024
2 parents d8a1882 + ea42536 commit 5e8c253
Show file tree
Hide file tree
Showing 21 changed files with 1,445 additions and 1,632 deletions.
65 changes: 65 additions & 0 deletions .do/app-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
features:
- buildpack-stack=ubuntu-22
ingress:
rules:
- component:
name: backend
preserve_path_prefix: true
match:
path:
prefix: /api
- component:
name: web
match:
path:
prefix: /
name: programming-languages-forum
region: fra
databases:
- cluster_name: programmingforum
db_name: programming-languages
db_user: doadmin
engine: MYSQL
name: programmingforum
production: true
version: "8"
services:
- name: backend
envs:
- key: DB_HOST
scope: RUN_AND_BUILD_TIME
value: ${db.HOSTNAME}:${db.PORT}
- key: DB_NAME
scope: RUN_AND_BUILD_TIME
value: ${db.DATABASE}
- key: DB_USER
scope: RUN_AND_BUILD_TIME
value: ${db.USERNAME}
- key: DB_PASSWORD
scope: RUN_AND_BUILD_TIME
value: ${db.PASSWORD}
- key: SPRING_PROFILES_ACTIVE
scope: RUN_AND_BUILD_TIME
value: docker
http_port: 8080
image:
deploy_on_push:
enabled: true
registry_type: DOCR
repository: backend
tag: latest
instance_count: 1
instance_size_slug: professional-xs
- name: web
http_port: 80
image:
deploy_on_push:
enabled: true
registry_type: DOCR
repository: web
tag: latest
instance_count: 1
instance_size_slug: professional-xs
65 changes: 65 additions & 0 deletions .do/app-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
features:
- buildpack-stack=ubuntu-22
ingress:
rules:
- component:
name: backend
preserve_path_prefix: true
match:
path:
prefix: /api
- component:
name: web
match:
path:
prefix: /
name: programming-languages-staging
region: fra
databases:
- name: programming-languages
engine: MYSQL
version: "8.0"
production: true
cluster_name: programmingforum-staging
db_name: programmingforum
db_user: programmingforum
services:
- envs:
- key: DB_HOST
scope: RUN_AND_BUILD_TIME
value: ${db.HOSTNAME}:${db.PORT}
- key: DB_NAME
scope: RUN_AND_BUILD_TIME
value: ${db.DATABASE}
- key: DB_USER
scope: RUN_AND_BUILD_TIME
value: ${db.USERNAME}
- key: DB_PASSWORD
scope: RUN_AND_BUILD_TIME
value: ${db.PASSWORD}
- key: SPRING_PROFILES_ACTIVE
scope: RUN_AND_BUILD_TIME
value: docker
http_port: 8080
image:
deploy_on_push:
enabled: true
registry_type: DOCR
repository: backend
tag: staging
instance_count: 1
instance_size_slug: professional-xs
name: backend
- http_port: 80
image:
deploy_on_push:
enabled: true
registry_type: DOCR
repository: web
tag: staging
instance_count: 1
instance_size_slug: professional-xs
name: web
4 changes: 2 additions & 2 deletions .github/workflows/backend_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: backend-test
name: Back-End CI

on:
push:
Expand All @@ -14,6 +14,7 @@ on:

jobs:
test:
name: Backend Tests
runs-on: ubuntu-latest
env:
working-directory: backend
Expand All @@ -29,4 +30,3 @@ jobs:

- name: Run the tests
run: docker run backend-test mvn test

44 changes: 44 additions & 0 deletions .github/workflows/frontend_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Front-End CI

on:
push:
paths:
- "frontend/**"
- ".github/workflows/frontend_test.yml"
pull_request:
branches:
- main
- staging

jobs:
test:
name: Frontend Tests
runs-on: ubuntu-latest
env:
working-directory: frontend
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v4

- name: Enable corepack
run: corepack enable

- name: Use Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: "20.x"
cache: 'yarn'

- name: Install dependencies
run: yarn install --immutable

- name: Run formatter
run: yarn format

- name: Run linter
run: yarn lint

- name: Run tests
run: yarn test
45 changes: 45 additions & 0 deletions .github/workflows/prod_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Deploy Production

on:
push:
branches:
- 'main'

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# Install doctl.
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DO_ACCESS_TOKEN }}

# Build a Docker image of your application in your registry and tag the image with the $GITHUB_SHA.
- name: Build backend image
run: docker build -t registry.digitalocean.com/programming-languages/backend:$(echo $GITHUB_SHA | head -c7) ./backend

- name: Build web image
run: docker build -t registry.digitalocean.com/programming-languages/web:$(echo $GITHUB_SHA | head -c7) ./frontend

- name: Log in to DigitalOcean Container Registry with short-lived credentials
run: doctl registry login --expiry-seconds 1200

# Push the Docker image to your registry.

- name: Push backend image
run: docker push registry.digitalocean.com/programming-languages/backend:$(echo $GITHUB_SHA | head -c7)

- name: Push web image
run: docker push registry.digitalocean.com/programming-languages/web:$(echo $GITHUB_SHA | head -c7)

# TODO: push to latest

- name: Push backend image
run: docker push registry.digitalocean.com/programming-languages/backend:latest

- name: Push web image
run: docker push registry.digitalocean.com/programming-languages/web:latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
personal_work/

node_modules

.yarn/install-state.gz
5 changes: 0 additions & 5 deletions .yarnrc

This file was deleted.

1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,24 @@ docker compose build

```bash
# for prod
docker tag bounswe2024group1-web:latest registry.digitalocean.com/semantic-browse/web:latest
docker tag bounswe2024group1-backend:latest registry.digitalocean.com/semantic-browse/backend:latest
docker tag bounswe2024group1-451-web:latest registry.digitalocean.com/programming-languages/web:latest
docker tag bounswe2024group1-451-backend:latest registry.digitalocean.com/programming-languages/backend:latest

# for staging
docker tag bounswe2024group1-web:latest registry.digitalocean.com/semantic-browse/web-staging:latest
docker tag bounswe2024group1-backend:latest registry.digitalocean.com/semantic-browse/backend-staging:latest
docker tag bounswe2024group1-451-web:latest registry.digitalocean.com/programming-languages/web:staging
docker tag bounswe2024group1-451-backend:latest registry.digitalocean.com/programming-languages/backend:staging
```

3. Push images to the registry.

```bash
# for prod
docker push registry.digitalocean.com/semantic-browse/web:latest
docker push registry.digitalocean.com/semantic-browse/backend:latest
docker push registry.digitalocean.com/programming-languages/web:latest
docker push registry.digitalocean.com/programming-languages/backend:latest

# for staging
docker push registry.digitalocean.com/semantic-browse/web-staging:latest
docker push registry.digitalocean.com/semantic-browse/backend-staging:latest
docker push registry.digitalocean.com/programming-languages/web:staging
docker push registry.digitalocean.com/programming-languages/backend:staging
```

This will trigger a deployment on the DigitalOcean backend.
Expand Down Expand Up @@ -184,7 +184,7 @@ To setup a new registry for this application, follow the steps below:
1. Create a new registry in the DigitalOcean dashboard.
2. Make sure it has enough space. (At least 1-2GB needed for staging and prod)
3. (IMPORTANT) Change the repository references to be the new registry in the app.yaml files.
4. (IMPORTANT) When deploying, change the registry references in the commands with the new registry. Our registry is called `semantic-browse` so rename that in the commands.
4. (IMPORTANT) When deploying, change the registry references in the commands with the new registry. Our registry is called `programming-languages` so rename that in the commands.

#### DB Containers

Expand All @@ -193,7 +193,7 @@ We use a mysql:latest container deployed on a Droplet for our database. To set t
Replace the variables with your secrets, make sure they're consistent with the declared environment variables in the app.yaml file.

```bash
docker run -d --name semantic-browse-db --network host -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_DATABASE=semantic_browse -e MYSQL_USER=semantic_browse -e MYSQL_PASSWORD=<user-password> -v semantic-browse-db:/var/lib/mysql mysql:latest
docker run -d --name programming-languages-db --network host -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_DATABASE=programminglanguages -e MYSQL_USER=semantic_browse -e MYSQL_PASSWORD=<user-password> -v programming-languages-db:/var/lib/mysql mysql:latest
```

Expose this container to the internet using DigitalOcean's firewall rules.
Expand Down
30 changes: 30 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use a base image with Maven to build the Spring Boot application
FROM maven:3.8.4 AS builder

# Set the working directory in the container
WORKDIR /app

# Copy the Maven project file
COPY pom.xml .

# Download dependencies
RUN mvn -B dependency:go-offline

# Copy the entire project source
COPY src ./src

# Build the application JAR (Skip tests since they require db)
RUN mvn -B package -DskipTests

# Use a lighter base image for the runtime environment
FROM openjdk:23-ea-17-slim

# Set the working directory in the container
WORKDIR /app

# Copy the JAR file from the previous stage
COPY --from=builder /app/target/ProgrammingLanguagesForum*.jar ./ProgrammingLanguagesForum.jar

EXPOSE 8080
# Command to run the application when the container starts
CMD ["java", "-jar", "ProgrammingLanguagesForum.jar"]
Empty file modified backend/mvnw
100644 → 100755
Empty file.
4 changes: 3 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: bounswe2024group1-451

services:
s3mock:
profiles:
- s3
image: adobe/s3mock:latest
environment:
- retainFilesOnExit=true
Expand Down Expand Up @@ -46,8 +48,8 @@ services:
depends_on:
- db
web:
platform: linux/amd64
build: ./frontend
platform: linux/amd64
volumes:
# these binds are required to reverse proxy the backend through nginx
# in prod, this is configured through digitalocean
Expand Down
2 changes: 2 additions & 0 deletions dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: bounswe2024group1-451-dev

services:
s3mock:
profiles:
- dev
extends:
file: compose.yml
service: s3mock
Expand Down
1 change: 1 addition & 0 deletions frontend/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cd frontend
yarn install --immutable
yarn test run
yarn lint-staged
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@
"prettier --write"
]
},
"packageManager": "yarn@4.2.2"
"packageManager": "yarn@4.5.0"
}
Loading

0 comments on commit 5e8c253

Please sign in to comment.