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
66 changes: 52 additions & 14 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
name: CD - Deploy to Server
name: CD

on:
push:
branches: [ "main" ]

env:
DEPLOYMENT_NAME: team5-app
REGISTRY: ghcr.io
REMOTE_TARGET_DIR: k8s

jobs:
deploy:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

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

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

- name: Build with Gradle
run: |
chmod +x gradlew
./gradlew clean build -x test

- name: Lowercase Image Name
run: |
echo "IMAGE_NAME=${REGISTRY,,}/${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
env:
REGISTRY: ${{ env.REGISTRY }}
GITHUB_REPOSITORY: ${{ github.repository }}

- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -26,21 +51,34 @@ jobs:
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:latest
tags: |
${{ env.IMAGE_NAME }}:latest
${{ env.IMAGE_NAME }}:${{ github.sha }}

- name: Copy k8s to Server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: 22
source: "k8s/*"
target: "/home/${{ secrets.SERVER_USER }}/k8s"

- name: Deploy to Server via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: 22
script: |
# 1. 최신 이미지 받아오기
docker pull ghcr.io/${{ github.repository }}:latest

# 2. 기존 컨테이너 끄고 새 컨테이너 띄우기 (docker-compose 사용)
cd ~/app # 서버 내 프로젝트 폴더 위치
docker-compose up -d

# 3. 안 쓰는 이미지 정리 (용량 확보)
docker image prune -f
DEPLOYMENT=${{ env.DEPLOYMENT_NAME }}
IMAGE=${{ env.IMAGE_NAME }}:latest
K8S_DIR="/home/${{ secrets.SERVER_USER }}/k8s"

sudo k3s crictl pull $IMAGE
sudo k3s kubectl apply -f $K8S_DIR/
sudo k3s kubectl rollout restart deployment $DEPLOYMENT
sudo k3s kubectl rollout status deployment $DEPLOYMENT
sudo k3s crictl rmi --prune
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Tests
name: CI

on:
push:
Expand All @@ -21,6 +21,21 @@ jobs:
--health-timeout 5s
--health-retries 20

mysql:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: toyproject-team5
MYSQL_USER: waffle
MYSQL_PASSWORD: somepassword
options: >-
--health-cmd "mysqladmin ping -h localhost -uroot -proot"
--health-interval 10s
--health-timeout 5s
--health-retries 10

steps:
- uses: actions/checkout@v4

Expand All @@ -30,8 +45,11 @@ jobs:
java-version: '17'
distribution: 'temurin'

- name: Execute permission for gradlew
run: chmod +x gradlew

- name: Run ktlint
run: ./gradlew ktlintCheck

- name: Run Tests
run: ./gradlew test
run: ./gradlew test
50 changes: 33 additions & 17 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
version: '3.8'

services:
# MySQL: JDBC와 연결될 DB
db:
app:
container_name: toyproject-team5
build: .
ports:
- "8080:8080"
depends_on:
- mysql
- redis
environment:
# 1. DB 설정
DB_HOST: mysql
DB_PORT: 3306
DB_NAME: toyproject-team5
DB_USER: waffle
DB_PASSWORD: somepassword

# 2. Redis 설정
REDIS_HOST: redis
REDIS_PORT: 6379

# 3. 기타 설정
JWT_SECRET: "secret-key-for-local-development-only"
S3_BUCKET_NAME: "dummy-bucket"

mysql:
image: mysql:8.0
container_name: mysql
container_name: mysql-db
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
volumes:
- ./mysql_data:/var/lib/mysql
command:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
MYSQL_DATABASE: toyproject-team5
MYSQL_ROOT_PASSWORD: root

MYSQL_USER: waffle
MYSQL_PASSWORD: somepassword

# Redis: JWT 블랙리스트나 캐싱용
redis:
image: redis:alpine # 가장 가벼운 버전
container_name: redis
image: redis:alpine
container_name: redis-cache
ports:
- "6379:6379"
8 changes: 4 additions & 4 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
# DB 설정 (JDBC)
datasource:
url: jdbc:mysql://localhost:3307/${DB_NAME:seminar2025}?allowPublicKeyRetrieval=true&useSSL=false
username: ${DB_USER:user}
url: jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3307}/${DB_NAME:toyproject-team5}?allowPublicKeyRetrieval=true&useSSL=false
username: ${DB_USER:waffle}
password: ${DB_PASSWORD:somepassword}
driver-class-name: com.mysql.cj.jdbc.Driver

Expand All @@ -19,15 +19,15 @@ spring:
cloud:
aws:
s3:
bucket: ${S3_BUCKET_NAME:my-studygroup-bucket}
bucket: ${S3_BUCKET_NAME:dummy-bucket}
region:
static: ap-northeast-2
stack:
auto: false

# JWT 설정
jwt:
secret: ${JWT_SECRET:your-super-secret-key-at-least-32-characters-long}
secret: ${JWT_SECRET:secret-key-for-local-development-only}
expiration-in-ms: 86400000

# Swagger
Expand Down
Loading