Skip to content

Commit

Permalink
feat: set CI/CD on main, CI on release-dev (#14)
Browse files Browse the repository at this point in the history
* feat: Test github workflow

* fix: ddl_auto - create_drop

- 이제 테스트가 데이터베이스에 영향을 주지 않습니다.

* feat: Use h2 on build

* fix: Use test profile on build

* fix: h2 Dialect sync

* revert: h2 use on test

* fix: use .env & refactor actions

- Just for test & dev
- Need to be fixed before prod

* fix: apply env on build workflow

* fix: use seperate test mysql container for build

* fix: Rename ssh key

* fix: Need ~/app folder, add mkdir command

* fix: continue on error - stop running appliation on EC2

* fix: Now deploy w/docker

* fix: run -> script typo

* fix: typo on keyname

* fix: fixed typo

* fix: fixed typo on keyname

* fix: fix dockerfile dir

* fix: typo on dockerfile name

* fix: typo on docker script

* fix: docker compose env injection

* fix: typo and unused .env create

* fix: rewrite invalid env injection command

* fix: fixed pwd typo, add --no-deps on docker compose up

* fix: pwd change

* fix: pwd typo

* fix: remove hardcoded docker-compose environment

* fix: change exposed port

* fix: env_file import

* fix: typo

* feat: set deploy on main, build on main & release-dev
  • Loading branch information
Grantzile authored Jan 15, 2025
1 parent 50b3950 commit d02217a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Build & deploy

on:
push:
branches:
- main
- release-dev

jobs:
deploy:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb
MYSQL_USER: user
MYSQL_PASSWORD: somepassword
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h localhost"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Build with Gradle
run: |
chmod +x gradlew
./gradlew build
env:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/testdb
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: somepassword
SPRING_JPA_HIBERNATE_DDL_AUTO: create-drop
SPRING_PROFILES_ACTIVE: "prod"

# 배포는 main 브랜치에서만 실행
- name: Upload JAR to EC2
if: github.ref == 'refs/heads/main'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "build/libs/*-SNAPSHOT.jar"
target: "~/app/"

- name: Delete latest JAR file
if: github.ref == 'refs/heads/main'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
cd ~/app
LATEST_JAR=$(ls -t *.jar | head -n 1) || true
echo "Latest JAR file: $LATEST_JAR"
for file in *.jar; do
if [[ "$file" != "$LATEST_JAR" ]]; then
echo "Deleting $file..."
rm -f "$file"
fi
done
continue-on-error: true

- name: Upload Dockerfile and Compose file
if: github.ref == 'refs/heads/main'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
source: "deploy-Dockerfile, docker-compose.yaml"
target: "~"

- name: Deploy via SSH
if: github.ref == 'refs/heads/main'
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
echo "Starting containers..."
docker compose up -d --no-deps myapp
7 changes: 7 additions & 0 deletions deploy-Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openjdk:17

# 가장 최신의 JAR 파일 하나만 있음, app.jar로 복사
COPY app/build/libs/*.jar /app.jar

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]
11 changes: 6 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ services:
networks:
- my-network
myapp:
build:
context: .
dockerfile: deploy-Dockerfile
image: myapp:1.0
env_file:
- .env
restart: on-failure
container_name: myapp-container
ports:
- "8080:8080"
- "80:8080"
depends_on:
- mysql
environment:
SPRING_DATASOURCE_URL: "jdbc:mysql://mysql-db:3306/testdb"
SPRING_DATASOURCE_USERNAME: "user"
SPRING_DATASOURCE_PASSWORD: "somepassword"
networks:
- my-network
networks:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ spring:
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: false
show_sql: false
ddl_auto: create-drop

0 comments on commit d02217a

Please sign in to comment.