diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a47788a..809caa3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,24 +9,71 @@ on: jobs: eslint-check: runs-on: self-hosted - timeout-minutes: 10 + timeout-minutes: 2 steps: - # 코드 가져오기 - name: Checkout code - uses: actions/checkout@v3 - - # Node.js 환경 설정 - - name: Setup Node.js - uses: actions/setup-node@v4 + uses: actions/checkout@v4 with: - node-version: '18' - cache: 'npm' + fetch-depth: 2 # HEAD와 베이스 브랜치 히스토리 확보 + + - name: Lightning fast lint check + run: | + echo "⚡ Quick lint check..." + + # 메모리 상태 사전 체크 + AVAILABLE_MB=$(free -m | grep ^Mem | awk '{print $7}') + echo "💾 Available memory: ${AVAILABLE_MB}MB" + + if [ "${{ github.event_name }}" = "pull_request" ]; then + # PR인 경우: 베이스 브랜치와 비교 + BASE_BRANCH="${{ github.base_ref }}" + git fetch origin $BASE_BRANCH --depth=1 + + ALL_CHANGED=$(git diff --name-only origin/$BASE_BRANCH...HEAD -- '*.ts' '*.tsx' '*.js' '*.jsx') + FILE_COUNT=$(echo "$ALL_CHANGED" | wc -w) + + echo "📊 Base: $BASE_BRANCH, Changed files: $FILE_COUNT" + + # 🛡️ 메모리 기반 안전장치 + if [ "$AVAILABLE_MB" -lt 200 ]; then + MAX_FILES=10 + echo "⚠️ Low memory mode: checking $MAX_FILES files only" + elif [ "$FILE_COUNT" -gt 30 ]; then + MAX_FILES=30 + echo "⚠️ Large PR detected: checking first $MAX_FILES files for memory safety" + echo "ℹ️ Note: Full project will be built during deployment" + else + MAX_FILES="$FILE_COUNT" + echo "✅ Normal mode: checking all $FILE_COUNT files" + fi + + CHANGED_FILES=$(echo "$ALL_CHANGED" | head -$MAX_FILES) + + else + # Push인 경우: 이전 커밋과 비교 + echo "🔍 Push event detected" + if git rev-parse --verify HEAD~1 >/dev/null 2>&1; then + CHANGED_FILES=$(git diff --name-only HEAD~1 -- '*.ts' '*.tsx' '*.js' '*.jsx') + echo "📝 Checking files changed since last commit" + else + echo "🔍 First commit or shallow clone, checking recent files" + CHANGED_FILES=$(find . -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" | grep -v node_modules | head -5) + fi + fi + + # 변경사항 없으면 즉시 종료 + if [ -z "$CHANGED_FILES" ]; then + echo "✅ No JS/TS files to check" + exit 0 + fi - # 의존성 설치 - - name: Install dependencies - run: npm ci + # 최종 실행 정보 + FINAL_COUNT=$(echo $CHANGED_FILES | wc -w) + ESTIMATED_MB=$((FINAL_COUNT * 2)) + echo "🧠 Checking $FINAL_COUNT files (~${ESTIMATED_MB}MB estimated)" + echo "📝 Files: $CHANGED_FILES" - # ESLint 실행 - - name: Run ESLint - run: npm run lint + # 글로벌 ESLint 실행 + npx eslint $CHANGED_FILES + echo "✅ Lint check passed!" diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d9aa8db..abda3ce 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,54 +3,74 @@ name: Deploy to EC2 on: pull_request: types: [closed] - branches: [main, develop] + branches: [main] # main 브랜치만 배포 + +env: + DOCKER_CONTAINER_NAME: coplan-app jobs: - deploy: + warmup: + # 🛡️ 머지된 경우에만 실행 if: github.event.pull_request.merged == true runs-on: self-hosted - timeout-minutes: 15 + timeout-minutes: 3 + steps: + - name: Environment check + run: | + echo "=== Deployment System Check ===" + echo "📋 Merged PR: ${{ github.event.pull_request.title }}" + echo "👤 Author: ${{ github.event.pull_request.user.login }}" + echo "🔄 From: ${{ github.event.pull_request.head.ref }} → ${{ github.event.pull_request.base.ref }}" + echo "📝 Commit: ${{ github.event.pull_request.merge_commit_sha }}" + echo "" + echo "🖥️ System Status:" + whoami + docker --version + free -h + echo "" + echo "🐳 Container Status:" + docker ps + deploy: + needs: warmup + runs-on: self-hosted + timeout-minutes: 10 steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: Deploy to EC2 + - name: Execute deployment script run: | echo "🚀 Starting deployment..." echo "🔄 Branch: ${{ github.ref_name }}" echo "🔄 Commit: ${{ github.sha }}" + echo "📦 PR: #${{ github.event.pull_request.number }}" - # 메모리 정리 - echo "🧹 Cleaning up memory..." - docker system prune -f - - # 앱 디렉토리로 코드 복사 - echo "📂 Copying files..." - rsync -av --delete \ - --exclude='.git' \ - --exclude='node_modules' \ - --exclude='.next' \ - ./ /home/ubuntu/coplan/app/ - - # 배포 디렉토리로 이동 + # deploy.sh 실행 cd /home/ubuntu/coplan + chmod +x ./deploy.sh + ./deploy.sh - # Docker 컨테이너 재시작 - echo "🔄 Restarting containers..." - docker compose down - docker compose up -d --build - - # 헬스체크 + - name: Health check + run: | echo "🏥 Health checking..." sleep 15 - if docker ps | grep -q coplan-app; then + if docker ps | grep -q $DOCKER_CONTAINER_NAME; then echo "✅ Deployment completed successfully!" - docker logs --tail 10 coplan-app - echo "🌐 Service available at: http://15.164.127.149" + echo "📊 Container status:" + docker ps | grep $DOCKER_CONTAINER_NAME + echo "" + echo "📝 Recent logs:" + docker logs --tail 5 $DOCKER_CONTAINER_NAME + echo "" + echo "🌐 Service available at: https://coplan.work" else echo "❌ Deployment failed!" - docker logs coplan-app + echo "🔍 Checking container logs..." + docker logs $DOCKER_CONTAINER_NAME 2>/dev/null || echo "No container logs available" + echo "🔍 Checking system status..." + docker ps -a + free -h exit 1 fi