9fdec36758
Deploy / deploy (push) Failing after 15m48s
@SpringBootTest(contextLoads) 시작 시 DefaultCategorySeeder 가 테이블을 조회하는데
CI 는 SQL_INIT_MODE 미설정(never)이라 스키마가 없어 BadSqlGrammarException 으로 실패했음.
운영과 동일하게 always 로 초기화 → 애플 로그인 커밋(a10e16c) 배포도 함께 통과.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
99 lines
3.9 KiB
YAML
99 lines
3.9 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
|
|
|
|
# clean build 가 테스트(@SpringBootTest 컨텍스트 로드 포함)를 돌리므로 CI와 동일한 DB/Redis 필요
|
|
services:
|
|
mariadb:
|
|
image: mariadb:11
|
|
env:
|
|
MARIADB_ROOT_PASSWORD: ci_root_pw
|
|
MARIADB_DATABASE: sb_db
|
|
options: >-
|
|
--health-cmd="healthcheck.sh --connect --innodb_initialized"
|
|
--health-interval=10s
|
|
--health-timeout=5s
|
|
--health-retries=15
|
|
redis:
|
|
image: redis:7
|
|
options: >-
|
|
--health-cmd="redis-cli ping"
|
|
--health-interval=10s
|
|
--health-timeout=5s
|
|
--health-retries=10
|
|
|
|
env:
|
|
DB_URL: jdbc:mariadb://mariadb:3306/sb_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Seoul
|
|
DB_USERNAME: root
|
|
DB_PASSWORD: ci_root_pw
|
|
REDIS_HOST: redis
|
|
REDIS_PORT: '6379'
|
|
# 컨텍스트 테스트(@SpringBootTest)가 스키마를 필요로 함(시작 시 기본분류 시더가 테이블 조회) → 운영과 동일하게 스키마 초기화
|
|
SQL_INIT_MODE: always
|
|
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup JDK 17
|
|
uses: actions/setup-java@v4
|
|
with:
|
|
distribution: temurin
|
|
java-version: '17'
|
|
cache: gradle
|
|
|
|
# clean build 는 test 를 포함 — 테스트 실패 시 jar 가 생성되지 않아 배포 중단(게이트)
|
|
- name: Build jar (테스트 포함)
|
|
run: |
|
|
chmod +x ./gradlew
|
|
./gradlew --no-daemon clean build
|
|
|
|
- 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" | tr -d '\r' > ~/.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'"
|
|
# 기동 대기: 최대 ~40초 폴링, 안 뜨면 상태 덤프 후 실패
|
|
$SSH "$DEPLOY_USER@$DEPLOY_HOST" "for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do systemctl is-active --quiet '$SERVICE' && echo 'service active' && exit 0; sleep 2; done; echo '서비스가 active 상태가 아닙니다:'; systemctl --no-pager -l status '$SERVICE' | tail -30; exit 1"
|
|
echo "✅ 배포 완료"
|