Skip to content

Commit 77c461e

Browse files
committed
chore: GitHub Actions 기반 EC2 자동 배포
1 parent 90f8545 commit 77c461e

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

.github/workflows/deploy.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Deploy to EC2
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- chore/#4
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
# 1) 코드 가져오기
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
# 2) JDK 설정
18+
- name: Set up JDK 21
19+
uses: actions/setup-java@v4
20+
with:
21+
distribution: 'temurin'
22+
java-version: '21'
23+
cache: gradle
24+
25+
# 3) Gradle 빌드
26+
- name: Build with Gradle
27+
run: chmod +x ./gradlew && ./gradlew clean bootjar
28+
29+
# 5) JAR를 EC2로 복사
30+
- name: Copy jar to EC2
31+
uses: appleboy/scp-action@v0.1.7
32+
with:
33+
host: ${{ secrets.EC2_HOST }}
34+
username: ${{ secrets.EC2_USER }}
35+
key: ${{ secrets.EC2_SSH_KEY }}
36+
source: "build/libs/*.jar"
37+
target: "/home/ubuntu/app"
38+
overwrite: true
39+
40+
# 6) EC2에서 앱 재시작
41+
- name: Restart Spring Boot app on EC2
42+
uses: appleboy/ssh-action@v1.0.3
43+
with:
44+
host: ${{ secrets.EC2_HOST }}
45+
username: ${{ secrets.EC2_USER }}
46+
key: ${{ secrets.EC2_SSH_KEY }}
47+
script: |
48+
echo "Stopping existing application if running..."
49+
if pgrep -f "java -jar"; then
50+
pkill -f "java -jar"
51+
sleep 5
52+
fi
53+
54+
echo "Finding latest JAR..."
55+
JAR_PATH=$(ls -t /home/ubuntu/app/*.jar | head -n 1)
56+
echo "Using JAR: $JAR_PATH"
57+
58+
echo "Export DB environment variable..."
59+
export DB_PASSWORD='${{ secrets.DB_PASSWORD }}'
60+
61+
echo "Starting application..."
62+
nohup java -jar "$JAR_PATH" \
63+
> /home/ubuntu/app/app.log 2>&1 &
64+
65+
sleep 3
66+
if pgrep -f "java -jar"; then
67+
echo "Application started successfully."
68+
else
69+
echo "Application failed to start."
70+
exit 1
71+
fi

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description = 'Demo project for Spring Boot'
1010

1111
java {
1212
toolchain {
13-
languageVersion = JavaLanguageVersion.of(25)
13+
languageVersion = JavaLanguageVersion.of(21)
1414
}
1515
}
1616

0 commit comments

Comments
 (0)