Skip to content

Commit 3c3e4cb

Browse files
authored
Refactor GitHub Actions workflow for EC2 deployment
1 parent 20bd62e commit 3c3e4cb

File tree

1 file changed

+19
-63
lines changed

1 file changed

+19
-63
lines changed

.github/workflows/deploy.yml

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,26 @@
1-
name: Build & Deploy Backend (GHCR → EC2)
1+
name: Deploy to EC2
22

33
on:
44
push:
5-
branches: [ main ]
6-
7-
permissions:
8-
contents: read
9-
packages: write
10-
11-
# (선택) 동시 배포 충돌 방지
12-
concurrency:
13-
group: debugvisual-ec2-deploy
14-
cancel-in-progress: true
5+
branches:
6+
- main # main 브랜치 push 시 자동 배포
157

168
jobs:
17-
build-push-deploy:
9+
deploy:
10+
name: Deploy to EC2 Server
1811
runs-on: ubuntu-latest
19-
steps:
20-
- uses: actions/checkout@v4
21-
22-
# (Spring) 빌드
23-
- uses: actions/setup-java@v4
24-
with:
25-
distribution: temurin
26-
java-version: 17
27-
- uses: actions/cache@v4
28-
with:
29-
path: |
30-
~/.gradle/caches
31-
~/.gradle/wrapper
32-
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*','**/gradle-wrapper.properties') }}
33-
- run: ./gradlew clean bootJar --no-daemon
3412

35-
# GHCR 로그인(액션에서만 필요)
36-
- uses: docker/setup-buildx-action@v3
37-
- uses: docker/login-action@v3
38-
with:
39-
registry: ghcr.io
40-
username: ${{ github.actor }}
41-
password: ${{ secrets.GITHUB_TOKEN }}
42-
43-
# 이미지 푸시 (latest + sha)
44-
- uses: docker/build-push-action@v6
45-
with:
46-
context: .
47-
push: true
48-
tags: |
49-
ghcr.io/dmu-debugvisual/debugvisual-backend:latest
50-
ghcr.io/dmu-debugvisual/debugvisual-backend:${{ github.sha }}
51-
52-
# EC2 배포 (익명 pull)
53-
- name: Deploy on EC2 (compose pull/up)
54-
uses: appleboy/ssh-action@v1.2.0
55-
with:
56-
host: ${{ secrets.EC2_HOST }}
57-
username: ${{ secrets.EC2_USER }} # ec2-user
58-
key: ${{ secrets.EC2_KEY }} # PEM 전체 내용
59-
script: |
60-
set -e
61-
LOCK=/tmp/debugvisual.deploy.lock
62-
(
63-
flock -n 9 || { echo 'Another deploy is running. Skip.'; exit 0; }
64-
cd ~/apps/debugvisual
65-
docker logout ghcr.io || true # 익명 pull 강제
66-
docker compose pull --ignore-pull-failures backend
67-
docker compose up -d --wait --remove-orphans backend
68-
docker image prune -af || true
69-
curl -skI https://api.zivorp.com/healthz | head -n1
70-
) 9>$LOCK
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up SSH key
18+
run: |
19+
mkdir -p ~/.ssh
20+
echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa
21+
chmod 600 ~/.ssh/id_rsa
22+
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
23+
24+
- name: SSH and deploy
25+
run: |
26+
ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "cd ~ && ./deploy.sh"

0 commit comments

Comments
 (0)