Skip to content

Commit a56edce

Browse files
committed
Build: Add CI/CD workflow for MADII-Server deployment
1 parent 6838814 commit a56edce

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

.github/workflows/oci_deploy.yml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: MADII-Server CI/CD
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
workflow_dispatch:
9+
10+
env:
11+
DOCKER_IMAGE: ${{ secrets.DOCKERHUB_REPOSITORY }} # 예: 9uttery/madii-server
12+
13+
jobs:
14+
build-and-deploy:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
21+
- name: Set up JDK 17
22+
uses: actions/setup-java@v4
23+
with:
24+
distribution: temurin
25+
java-version: 17
26+
27+
- name: Grant execute permission for gradlew
28+
run: chmod +x ./gradlew
29+
shell: bash
30+
31+
# ===== application-*.yml, Firebase 키 생성 (기존과 동일) =====
32+
- name: make application-secret.yml
33+
run: echo "${{ secrets.APPLICATION_SECRET_BASE64 }}" | base64 --decode > ./src/main/resources/application-secret.yml
34+
shell: bash
35+
36+
- name: make application-dev.yml
37+
run: echo "${{ secrets.APPLICATION_DEV_BASE64 }}" | base64 --decode > ./src/main/resources/application-dev.yml
38+
shell: bash
39+
40+
- name: make application-prod.yml
41+
run: echo "${{ secrets.APPLICATION_PROD_BASE64 }}" | base64 --decode > ./src/main/resources/application-prod.yml
42+
shell: bash
43+
44+
- name: create madii-app-firebase-adminsdk json
45+
id: create-json
46+
uses: jsdaniell/[email protected]
47+
with:
48+
name: "madii-app-firebase-adminsdk-uriyk-c04677456f.json"
49+
json: ${{ secrets.FIREBASE_KEY_JSON }}
50+
dir: "src/main/resources/"
51+
52+
- name: Gradle cache
53+
uses: actions/cache@v3
54+
with:
55+
path: |
56+
~/.gradle/caches
57+
~/.gradle/wrapper
58+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
59+
restore-keys: |
60+
${{ runner.os }}-gradle-
61+
62+
- name: Build with Gradle
63+
run: ./gradlew build
64+
shell: bash
65+
66+
# ====== Docker 이미지 빌드 & Docker Hub 푸시 ======
67+
- name: Build Docker image
68+
run: |
69+
IMAGE="${{ env.DOCKER_IMAGE }}:${GITHUB_SHA}"
70+
echo "Building image: $IMAGE"
71+
docker build -t "$IMAGE" .
72+
73+
- name: Login to Docker Hub
74+
run: |
75+
echo "${{ secrets.DOCKERHUB_TOKEN }}" \
76+
| docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
77+
78+
- name: Push Docker image
79+
run: |
80+
IMAGE="${{ env.DOCKER_IMAGE }}:${GITHUB_SHA}"
81+
docker push "$IMAGE"
82+
83+
# ====== OCI 인스턴스로 배포 (SSH) ======
84+
- name: Deploy to OCI instance
85+
uses: appleboy/[email protected]
86+
with:
87+
host: ${{ secrets.OCI_HOST }}
88+
username: ${{ secrets.OCI_SSH_USER }}
89+
key: ${{ secrets.OCI_SSH_KEY }}
90+
port: 22
91+
script: |
92+
set -e
93+
94+
IMAGE="${{ env.DOCKER_IMAGE }}:${GITHUB_SHA}"
95+
BRANCH="${{ github.ref_name }}"
96+
97+
# main -> prod, dev -> dev 프로파일
98+
if [ "$BRANCH" = "dev" ]; then
99+
PROFILE="dev"
100+
CONTAINER_NAME="madii-dev"
101+
else
102+
PROFILE="prod"
103+
CONTAINER_NAME="madii-prod"
104+
fi
105+
106+
echo "Deploying $IMAGE with profile=$PROFILE as container=$CONTAINER_NAME"
107+
108+
# 1) Docker 미설치 시 자동 설치
109+
if ! command -v docker >/dev/null 2>&1; then
110+
echo "Docker not found. Installing..."
111+
curl -fsSL https://get.docker.com | sudo sh
112+
fi
113+
114+
# 2) Docker Hub 로그인
115+
echo "${{ secrets.DOCKERHUB_TOKEN }}" \
116+
| sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
117+
118+
# 3) 최신 이미지 pull
119+
sudo docker pull "$IMAGE"
120+
121+
# 4) 기존 컨테이너 중지/삭제
122+
if sudo docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
123+
echo "Stopping old container..."
124+
sudo docker stop "$CONTAINER_NAME" || true
125+
sudo docker rm "$CONTAINER_NAME" || true
126+
fi
127+
128+
# 5) 새 컨테이너 실행
129+
sudo docker run -d \
130+
--name "$CONTAINER_NAME" \
131+
--restart unless-stopped \
132+
-p 8090:8090 \
133+
-e SPRING_PROFILES_ACTIVE="$PROFILE" \
134+
"$IMAGE"
135+
136+
echo "Deploy finished."

0 commit comments

Comments
 (0)