-
Notifications
You must be signed in to change notification settings - Fork 1
185 lines (160 loc) · 6.79 KB
/
Copy pathdev-deploy.yml
File metadata and controls
185 lines (160 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: DEV Deploy
on: # ① 트리거 조건 : develop에 push
push:
branches: [develop]
env: # ② 워크플로 전역 환경변수
APP_HOST: 10.0.2.177 # 개발용 App EC2 Private IP
NGINX_HOST: 10.0.1.18 # 공통 Nginx EC2 Private IP
TZ: Asia/Seoul # Runner 로그 타임존
jobs:
deploy:
runs-on: ubuntu-latest # GitHub Hosted Runner
steps:
- uses: actions/checkout@v4 # ③ 레포지토리 코드 체크아웃
- uses: actions/setup-java@v4 # ④ JDK 설치 + Gradle 캐시
with:
distribution: temurin
java-version: 17
cache: gradle
- name: Build JAR # ⑤ 컴파일
run: |
chmod +x ./gradlew && ./gradlew clean build -x test
- name: 🗂️ Prepare target directory on Nginx EC2
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.NGINX_HOST }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
set -e
sudo mkdir -p /home/ubuntu/nginx
sudo chown -R ubuntu:ubuntu /home/ubuntu/nginx
grep -E '^Subsystem' /etc/ssh/sshd_config || true
ls -al /home/ubuntu/nginx
- name: 📦 Copy NGINX bundle to Nginx EC2
uses: appleboy/scp-action@v0.1.6
with:
host: ${{ env.NGINX_HOST }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
source: "nginx/*"
target: "/home/ubuntu/nginx/"
strip_components: 1
debug: true
- name: 🧾 List files on Nginx EC2
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.NGINX_HOST }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
set -e
ls -al /home/ubuntu
echo "----"
ls -al /home/ubuntu/nginx
- name: 🔧 Ensure Nginx container is up (DEV)
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.NGINX_HOST }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
set -e
cd ~/nginx
sudo systemctl start docker
sudo systemctl enable docker >/dev/null 2>&1 || true
sudo docker compose -p hilingual up -d --remove-orphans nginx
- name: 📦 Copy JAR to Target
uses: appleboy/scp-action@v0.1.6
with:
host: ${{ env.APP_HOST }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
source: "build/libs/*.jar"
target: "/home/ubuntu/artifacts/"
strip_components: 2
- name: 🔑 Prepare upstream-switch key on App EC2
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ env.APP_HOST }}
username: ubuntu
key: ${{ secrets.SSH_KEY }}
proxy_host: ${{ secrets.BASTION_IP }}
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
script_stop: true
script: |
set -e
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# 프라이빗 키 저장
cat > ~/.ssh/hilingual_actions <<'EOF'
${{ secrets.SSH_KEY }}
EOF
# 🔧 잡음 제거 (Drone 등에서 섞여든 행, BOM 등)
LC_ALL=C sed -i -e '1s/^\xEF\xBB\xBF//' -e '/^DRONE_/d' ~/.ssh/hilingual_actions
# 🔒 BEGIN~END 블록만 남기고 나머지 잡음 제거 (재발 방지 핵심)
awk 'f||/-----BEGIN .* PRIVATE KEY-----/{f=1;print} /-----END .* PRIVATE KEY-----/{exit}' \
~/.ssh/hilingual_actions > ~/.ssh/hilingual_actions.clean && mv ~/.ssh/hilingual_actions.clean ~/.ssh/hilingual_actions
# 윈도우 개행 제거 + 권한
sed -i 's/\r$//' ~/.ssh/hilingual_actions
chmod 600 ~/.ssh/hilingual_actions
# 키 유효성 검사 (깨진 키면 즉시 실패 지점 명확화)
ssh-keygen -y -f ~/.ssh/hilingual_actions >/dev/null || { echo "Invalid SSH private key in secrets.SSH_KEY"; exit 1; }
# bastion에서 Nginx로 넘어갈 때 호스트 키 확인 프롬프트 방지
ssh-keyscan -H ${{ env.NGINX_HOST }} >> ~/.ssh/known_hosts 2>/dev/null || true
- name: 🔐 Deploy via Bastion(DEV) # ⑥ Bastion 경유 SSH 배포
uses: appleboy/ssh-action@v1.0.3
env:
APP_HOST: ${{ env.APP_HOST }} # deploy.sh 에 전달될 변수
NGINX_HOST: ${{ env.NGINX_HOST }}
UPSTREAM_ENV: dev
with:
host: ${{ env.APP_HOST }} # dev App EC2
username: ubuntu
key: ${{ secrets.SSH_KEY }} # Repo Secret
proxy_host: ${{ secrets.BASTION_IP }} # (Jump Host = 동일)
proxy_username: ubuntu
proxy_key: ${{ secrets.SSH_KEY }}
script_stop: true # 서버 오류 시 Job Fail.
envs: APP_HOST,NGINX_HOST,UPSTREAM_ENV
script: |
set -e
mkdir -p /home/ubuntu/artifacts
cd ~/project
if [ ! -d .git ]; then
git init
git remote add origin https://github.com/${{ github.repository }}
fi
git fetch --depth 1 origin
git reset --hard ${{ github.sha }}
mkdir -p build/libs
mv /home/ubuntu/artifacts/*.jar build/libs/
chmod +x deploy.sh
UPSTREAM_ENV=${UPSTREAM_ENV} APP_HOST=${APP_HOST} NGINX_HOST=${NGINX_HOST} ./deploy.sh
- name: 📢 Discord notify
if: always()
uses: sarisia/actions-status-discord@v1
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
title: "${{ github.workflow }} – ${{ job.status }}"
description: "**Ref**: ${{ github.ref_name }}\n**SHA**: ${{ github.sha }}"
color: ${{ job.status == 'success' && '0x57F287' || '0xED4245' }}