chore: 배포 설정 수정 #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to EC2 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| env: | |
| EC2_HOST: ${{ secrets.EC2_HOST }} | |
| EC2_USER: ${{ secrets.EC2_USER }} | |
| EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup SSH | |
| run: | | |
| # SSH 디렉토리 생성 | |
| mkdir -p ~/.ssh | |
| chmod 700 ~/.ssh | |
| # SSH 키 저장 | |
| echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| # known_hosts 설정 | |
| ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts 2>/dev/null || true | |
| chmod 644 ~/.ssh/known_hosts || true | |
| echo "✅ SSH setup completed" | |
| - name: Deploy to EC2 | |
| run: | | |
| set -e | |
| # EC2에서 git pull 및 배포 실행 | |
| ssh -o StrictHostKeyChecking=yes -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -i ~/.ssh/deploy_key ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' | |
| # 배포 디렉토리로 이동 | |
| cd ~/cinecollector || { | |
| echo "⚠️ 배포 디렉토리가 없습니다. 초기 설정을 진행합니다..." | |
| mkdir -p ~/cinecollector | |
| cd ~/cinecollector | |
| # Git 저장소 클론 (처음 배포인 경우) | |
| if [ ! -d ".git" ]; then | |
| echo "📥 Git 저장소 클론 중..." | |
| git clone https://github.com/${{ github.repository }}.git . | |
| fi | |
| } | |
| # Git pull로 최신 코드 가져오기 | |
| echo "📥 최신 코드 가져오는 중..." | |
| git fetch origin | |
| git reset --hard origin/main | |
| git clean -fd | |
| # 배포 스크립트 실행 | |
| if [ -f "scripts/deploy.sh" ]; then | |
| chmod +x scripts/deploy.sh | |
| ./scripts/deploy.sh | |
| else | |
| echo "⚠️ 배포 스크립트를 찾을 수 없습니다." | |
| exit 1 | |
| fi | |
| EOF | |
| - name: Cleanup | |
| if: always() | |
| run: | | |
| rm -f ~/.ssh/deploy_key |