614e74be8a
CI / build (push) Has been cancelled
- deploy.yaml: jar 빌드 → scp 전송 → systemctl 재시작 - deploy/sb-backend.service: systemd 유닛 샘플 - deploy/README.md: 시크릿·서버 준비 안내 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
66 lines
2.4 KiB
YAML
66 lines
2.4 KiB
YAML
name: Deploy
|
|
|
|
# main 푸시 시 자동 배포: jar 빌드 → 서버로 scp → systemd 재시작
|
|
#
|
|
# 저장소 Settings > Actions > Secrets 에 등록:
|
|
# DEPLOY_HOST 배포 서버 호스트/IP
|
|
# DEPLOY_USER SSH 계정 (systemctl 무비밀번호 sudo 필요 — deploy/README.md 참고)
|
|
# DEPLOY_SSH_KEY SSH 개인키 (PEM 전체)
|
|
# DEPLOY_PATH jar 배치 디렉터리 (예: /opt/sb-backend)
|
|
# DEPLOY_PORT SSH 포트 (선택, 기본 22)
|
|
# SERVICE_NAME systemd 유닛명 (선택, 기본 sb-backend)
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
workflow_dispatch: {}
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup JDK 17
|
|
uses: actions/setup-java@v4
|
|
with:
|
|
distribution: temurin
|
|
java-version: '17'
|
|
cache: gradle
|
|
|
|
- name: Build jar (skip tests)
|
|
run: |
|
|
chmod +x ./gradlew
|
|
./gradlew --no-daemon clean bootJar -x test
|
|
|
|
- name: Deploy (SSH + systemd)
|
|
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 }}
|
|
SERVICE_NAME: ${{ secrets.SERVICE_NAME }}
|
|
run: |
|
|
set -euo pipefail
|
|
PORT="${DEPLOY_PORT:-22}"
|
|
SERVICE="${SERVICE_NAME:-sb-backend}"
|
|
DEST="${DEPLOY_PATH:-}"
|
|
if [ -z "$DEST" ] || [ "$DEST" = "/" ]; then
|
|
echo "::error::DEPLOY_PATH 시크릿이 비어 있거나 잘못되었습니다."; exit 1
|
|
fi
|
|
|
|
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"
|
|
|
|
JAR=$(ls build/libs/*.jar | grep -v -- '-plain.jar' | head -n1)
|
|
echo "Deploy $JAR -> $DEPLOY_USER@$DEPLOY_HOST:$DEST/app.jar"
|
|
|
|
$SSH "$DEPLOY_USER@$DEPLOY_HOST" "mkdir -p '$DEST'"
|
|
scp -P "$PORT" -i "$HOME/.ssh/id_deploy" "$JAR" "$DEPLOY_USER@$DEPLOY_HOST:$DEST/app.jar"
|
|
$SSH "$DEPLOY_USER@$DEPLOY_HOST" "sudo systemctl restart '$SERVICE' && sleep 3 && sudo systemctl is-active '$SERVICE'"
|
|
echo "✅ 배포 완료"
|