Deploy to EC2 #62
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, master ] | |
| pull_request: | |
| branches: [ main, master ] | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run linter | |
| run: npm run lint | |
| - name: Build project | |
| run: npm run build | |
| deploy: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/sysone-fe:latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Deploy to EC2 with HTTPS | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| cd /home/ubuntu | |
| # 환경 변수 파일 생성/업데이트 | |
| echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" > .env | |
| # HTTPS Docker Compose 파일이 있는지 확인 | |
| if [ ! -f "docker-compose.https.yml" ]; then | |
| echo "❌ HTTPS 설정이 되어있지 않습니다!" | |
| echo "먼저 서버에서 add-https-to-existing.sh 스크립트를 실행해주세요." | |
| exit 1 | |
| fi | |
| # 기존 컨테이너 중지 및 제거 | |
| sudo docker-compose -f docker-compose.https.yml down || true | |
| # 오래된 이미지 제거 | |
| sudo docker rmi ${{ secrets.DOCKER_USERNAME }}/sysone-fe:latest || true | |
| # 최신 이미지 풀 및 실행 | |
| sudo docker-compose -f docker-compose.https.yml pull | |
| sudo docker-compose -f docker-compose.https.yml up -d | |
| # 사용하지 않는 이미지 정리 | |
| sudo docker image prune -f | |
| # 배포 상태 확인 | |
| sleep 10 | |
| sudo docker-compose -f docker-compose.https.yml ps | |
| echo "✅ HTTPS 배포 완료: https://sysonetaskmanager.store" |