From 09cfb8e562432c344b5da3fb2db054dc50a8440f Mon Sep 17 00:00:00 2001 From: ByungCheol Date: Sun, 31 May 2026 15:55:23 +0900 Subject: [PATCH] =?UTF-8?q?ci:=20=ED=94=84=EB=A1=A0=ED=8A=B8=20=EC=9E=90?= =?UTF-8?q?=EB=8F=99=20=EB=B0=B0=ED=8F=AC=20=EA=B5=AC=ED=98=84=20(Nginx=20?= =?UTF-8?q?=EC=A0=95=EC=A0=81=ED=98=B8=EC=8A=A4=ED=8C=85,=20main=20push)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - deploy.yaml: dist 빌드 → SSH(tar)로 웹 루트 교체 → nginx reload - deploy/nginx-sb-front.conf: SPA 폴백 + /api 프록시 샘플 - deploy/README.md: 시크릿·서버 준비 안내 Co-Authored-By: Claude Opus 4.8 --- .gitea/workflows/deploy.yaml | 57 ++++++++++++++++++++++++------------ deploy/README.md | 39 ++++++++++++++++++++++++ deploy/nginx-sb-front.conf | 28 ++++++++++++++++++ 3 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 deploy/README.md create mode 100644 deploy/nginx-sb-front.conf diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index 03270b6..f3a529f 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -1,12 +1,17 @@ -name: Deploy (manual) +name: Deploy -# 배포 대상이 정해지면 아래 단계의 주석을 풀고 시크릿을 등록하세요. -# 저장소 Settings > Actions > Secrets 에 등록 필요: -# DEPLOY_HOST, DEPLOY_USER, DEPLOY_SSH_KEY, DEPLOY_PATH +# main 푸시 시 자동 배포: dist 빌드 → 서버 Nginx 루트로 업로드 → nginx reload +# +# 저장소 Settings > Actions > Secrets 에 등록: +# DEPLOY_HOST 배포 서버 호스트/IP +# DEPLOY_USER SSH 계정 (nginx reload 무비밀번호 sudo 필요 — deploy/README.md 참고) +# DEPLOY_SSH_KEY SSH 개인키 (PEM 전체) +# DEPLOY_PATH Nginx 웹 루트 (예: /var/www/sb-front) +# DEPLOY_PORT SSH 포트 (선택, 기본 22) on: + push: + branches: [main] workflow_dispatch: {} - # push: - # tags: ['v*'] jobs: deploy: @@ -26,17 +31,31 @@ jobs: npm ci npm run build - # ===== 배포 예시 (택1) — 사용 시 주석 해제 ===== - # 1) 정적 파일을 원격 서버로 rsync (Nginx 등 정적 호스팅) - # - name: Deploy via SSH/rsync - # uses: appleboy/scp-action@v0.1.7 - # with: - # host: ${{ secrets.DEPLOY_HOST }} - # username: ${{ secrets.DEPLOY_USER }} - # key: ${{ secrets.DEPLOY_SSH_KEY }} - # source: "dist/*" - # target: ${{ secrets.DEPLOY_PATH }} - # strip_components: 1 + - name: Deploy to Nginx (SSH) + env: + DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }} + DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }} + DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} + run: | + set -euo pipefail + PORT="${DEPLOY_PORT:-22}" + DEST="${DEPLOY_PATH:-}" + if [ -z "$DEST" ] || [ "$DEST" = "/" ]; then + echo "::error::DEPLOY_PATH 시크릿이 비어 있거나 잘못되었습니다."; exit 1 + fi - - name: Notice - run: echo "dist 빌드 완료. 배포 대상 지정 후 위 단계를 활성화하세요." + mkdir -p ~/.ssh && chmod 700 ~/.ssh + printf '%s\n' "$DEPLOY_SSH_KEY" > ~/.ssh/id_deploy + chmod 600 ~/.ssh/id_deploy + ssh-keyscan -p "$PORT" "$DEPLOY_HOST" >> ~/.ssh/known_hosts 2>/dev/null + SSH="ssh -p $PORT -i $HOME/.ssh/id_deploy -o StrictHostKeyChecking=yes" + + echo "Deploy dist/ -> $DEPLOY_USER@$DEPLOY_HOST:$DEST" + $SSH "$DEPLOY_USER@$DEPLOY_HOST" "mkdir -p '$DEST'" + # 기존 파일 정리 후 새 빌드 업로드 (tar 파이프 — 추가 의존성 없음) + tar -czf - -C dist . | $SSH "$DEPLOY_USER@$DEPLOY_HOST" "find '$DEST' -mindepth 1 -delete && tar -xzf - -C '$DEST'" + # Nginx 설정 검증 후 reload (실패해도 배포 자체는 유지) + $SSH "$DEPLOY_USER@$DEPLOY_HOST" "sudo nginx -t && sudo nginx -s reload" || echo "nginx reload 생략됨" + echo "✅ 배포 완료" diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 0000000..a2c38c6 --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,39 @@ +# 프론트엔드 배포 (Nginx 정적 호스팅) + +`main` 푸시 시 [.gitea/workflows/deploy.yaml](../.gitea/workflows/deploy.yaml) 가 +`dist` 를 빌드해 서버 Nginx 웹 루트로 업로드하고 nginx 를 reload 합니다. + +## 1. Gitea 저장소 시크릿 (Settings → Actions → Secrets) +| 시크릿 | 설명 | 예시 | +|---|---|---| +| `DEPLOY_HOST` | 서버 호스트/IP | `10.0.0.5` | +| `DEPLOY_USER` | SSH 계정 | `deploy` | +| `DEPLOY_SSH_KEY` | SSH 개인키(PEM 전체) | `-----BEGIN OPENSSH PRIVATE KEY-----...` | +| `DEPLOY_PATH` | Nginx 웹 루트 | `/var/www/sb-front` | +| `DEPLOY_PORT` | SSH 포트(선택) | `22` | + +## 2. 서버 1회 준비 +```bash +sudo apt-get install -y nginx +sudo mkdir -p /var/www/sb-front && sudo chown deploy:deploy /var/www/sb-front + +# Nginx 서버블록 설치 (deploy/nginx-sb-front.conf 참고) +sudo cp deploy/nginx-sb-front.conf /etc/nginx/sites-available/sb-front +sudo ln -sf /etc/nginx/sites-available/sb-front /etc/nginx/sites-enabled/sb-front +sudo nginx -t && sudo systemctl reload nginx + +# deploy 계정에 nginx 검증/reload 무비밀번호 sudo 허용 +echo 'deploy ALL=(root) NOPASSWD: /usr/sbin/nginx -t, /usr/sbin/nginx -s reload' \ + | sudo tee /etc/sudoers.d/sb-front + +# 러너 공개키를 deploy 계정 authorized_keys 에 등록 (백엔드와 동일 키 사용 가능) +``` + +## 3. 동작 +- main 푸시 → CI(빌드) + Deploy(빌드·업로드·reload) 가 각각 실행 +- 업로드는 웹 루트를 비우고 새 `dist` 로 교체 (해시 자산명이라 안전) +- 수동 배포: 저장소 Actions → Deploy → Run workflow + +## 참고 +- API 는 같은 도메인의 `/api/` 로 Nginx 가 백엔드(8080)에 프록시합니다. + 별도 도메인/포트로 띄운다면 빌드시 `VITE_API_BASE_URL` 환경변수를 지정하세요. diff --git a/deploy/nginx-sb-front.conf b/deploy/nginx-sb-front.conf new file mode 100644 index 0000000..bf7a6a1 --- /dev/null +++ b/deploy/nginx-sb-front.conf @@ -0,0 +1,28 @@ +# /etc/nginx/sites-available/sb-front (심볼릭 링크로 sites-enabled 에 연결) +server { + listen 80; + server_name _; # 도메인으로 교체 + + root /var/www/sb-front; # DEPLOY_PATH 와 동일 + index index.html; + + # SPA: 새로고침/직접 진입 시 index.html 로 폴백 (vue-router history 모드) + location / { + try_files $uri $uri/ /index.html; + } + + # 해시 자산 장기 캐시 + location /assets/ { + expires 1y; + add_header Cache-Control "public, immutable"; + } + + # API 프록시 → 백엔드(8080) + location /api/ { + proxy_pass http://127.0.0.1:8080; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +}