Skip to content
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
102 changes: 102 additions & 0 deletions .github/workflows/backend-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Deploy Backend

on:
pull_request:
branches:
- dev
types:
- closed
paths:
- "backend/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build Application
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Provision JDK environment
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "17"
cache: "maven"

- name: Execute Maven build
working-directory: backend
run: mvn clean verify -Duser.timezone=Europe/Warsaw

- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: backend-dist
path: |
backend/target/*.jar
backend/Dockerfile
retention-days: 1

deploy:
name: Deploy to EC2
needs: build
runs-on: ubuntu-latest

steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: backend-dist
path: backend-dist

- name: Transfer artifacts to target environment
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "backend-dist/*"
target: "/tmp/backend-deploy"
strip_components: 1

- name: Orchestrate container deployment
uses: appleboy/[email protected]
env:
DB_URL: ${{ secrets.DB_URL }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
OPENROUTESERVICE_API_KEY: ${{ secrets.OPENROUTESERVICE_API_KEY }}
JWT_SERVICE_KEY: ${{ secrets.JWT_SERVICE_KEY }}
JWT_EXPIRATION: ${{ secrets.JWT_EXPIRATION }}
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
envs: DB_URL,DB_USERNAME,DB_PASSWORD,OPENROUTESERVICE_API_KEY,JWT_SERVICE_KEY,JWT_EXPIRATION
script: |
mkdir -p ~/BookRider/backend
mv /tmp/backend-deploy/* ~/BookRider/backend/
cd ~/BookRider/backend

echo "DB_URL=$DB_URL" > .env
echo "DB_USERNAME=$DB_USERNAME" >> .env
echo "DB_PASSWORD=$DB_PASSWORD" >> .env
echo "OPENROUTESERVICE_API_KEY=$OPENROUTESERVICE_API_KEY" >> .env
echo "JWT_SERVICE_KEY=$JWT_SERVICE_KEY" >> .env
echo "JWT_EXPIRATION=$JWT_EXPIRATION" >> .env

docker stop backend-container || true
docker rm backend-container || true
docker rmi backend-image || true
docker system prune -af --volumes

docker build -t backend-image .
docker run -d --restart unless-stopped -p 8080:8080 --env-file .env --name backend-container backend-image

rm -rf /tmp/backend-deploy
85 changes: 85 additions & 0 deletions .github/workflows/web-app-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Deploy Frontend

on:
pull_request:
branches:
- dev
types:
- closed
paths:
- "web_app/**"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
name: Build Application
runs-on: ubuntu-latest
defaults:
run:
working-directory: web_app

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: "npm"
cache-dependency-path: web_app/package-lock.json

- name: Generate environment variables
run: echo "VITE_API_BASE_URL=${{ secrets.API_BASE_URL }}" > .env

- name: Install dependencies
run: npm ci

- name: Build frontend
run: npm run build

- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: webapp-dist
path: web_app/dist/
retention-days: 1

deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: build
if: github.event.pull_request.merged == true

steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: webapp-dist
path: dist

- name: Transfer artifacts to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "dist/"
target: "/tmp/webapp"
strip_components: 1

- name: Update webroot and reload NGINX
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
sudo mkdir -p /var/www/myreactapp
sudo rsync -a --delete /tmp/webapp/ /var/www/myreactapp/
sudo chown -R www-data:www-data /var/www/myreactapp
sudo rm -rf /tmp/webapp
sudo systemctl reload nginx
12 changes: 5 additions & 7 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
FROM maven:3.9.4-eclipse-temurin-17 AS builder
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests

FROM eclipse-temurin:17-jdk-jammy
FROM eclipse-temurin:17-jre-jammy

ENV TZ=Europe/Warsaw
RUN apt-get update && \
Expand All @@ -14,6 +9,9 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar

COPY target/*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java", "-Duser.timezone=Europe/Warsaw", "-jar", "app.jar"]
9 changes: 8 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
Expand Down Expand Up @@ -121,6 +121,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
9 changes: 9 additions & 0 deletions backend/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring.config.import=

spring.datasource.url=jdbc:tc:postgresql:15-alpine:///testdb
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver

spring.datasource.username=test
spring.datasource.password=test

spring.jpa.hibernate.ddl-auto=create-drop
Loading