๐ fix: ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ผ๋ก ์ธํ ๋ฐฐํฌ ์คํจ ๋ฌธ์ #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: | |
| pull_request: | |
| types: [closed] | |
| branches: [main, develop] | |
| jobs: | |
| deploy: | |
| if: github.event.pull_request.merged == true | |
| runs-on: self-hosted | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Deploy to EC2 | |
| run: | | |
| set -e | |
| # ๋ฐฐํฌ ๋ก๊น | |
| echo "๐ Starting deployment..." | |
| echo "๐ PR: #${{ github.event.pull_request.number }}" | |
| echo "๐ Commit: ${{ github.sha }}" | |
| # ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ | |
| echo "๐งน Cleaning up Docker resources..." | |
| docker system prune -f | |
| # ๋ธ๋์น๋ณ ํ๊ฒฝ ์ค์ | |
| if [ "${{ github.event.pull_request.base.ref }}" = "main" ]; then | |
| CONTAINER_NAME="app-prod" | |
| ENV_FILE="app-prod.env" | |
| OTHER_CONTAINER="app-dev" | |
| echo "๐ Deploying to PRODUCTION" | |
| elif [ "${{ github.event.pull_request.base.ref }}" = "develop" ]; then | |
| CONTAINER_NAME="app-dev" | |
| ENV_FILE="app-dev.env" | |
| OTHER_CONTAINER="app-prod" | |
| echo "๐งช Deploying to DEVELOPMENT" | |
| fi | |
| # ๋ฐฐํฌ ๋๋ ํ ๋ฆฌ ์ค์ | |
| DEPLOY_DIR="/home/ubuntu/coplan/app" | |
| # ๋ฉ๋ชจ๋ฆฌ ํ๋ณด๋ฅผ ์ํด ๋ค๋ฅธ ์ปจํ ์ด๋ ์ค์ง | |
| echo "โธ๏ธ Stopping other container to free memory..." | |
| docker compose stop ${OTHER_CONTAINER} || true | |
| # rsync๋ก ํ์ผ ๋๊ธฐํ | |
| echo "๐ Synchronizing files..." | |
| rsync -av \ | |
| --exclude='.git' \ | |
| --exclude='node_modules' \ | |
| --exclude='.next' \ | |
| --exclude='.env*' \ | |
| --exclude='*.log' \ | |
| --delete \ | |
| ./ ${DEPLOY_DIR}/ | |
| # ์์ ๋๋ ํ ๋ฆฌ ์ด๋ | |
| cd /home/ubuntu/coplan | |
| # ํ๊ฒฝ๋ณ์ ํ์ผ ๋ณต์ฌ (์๋ ๊ฒฝ์ฐ๋ง) | |
| if [ -f "/home/ubuntu/env/${ENV_FILE}" ]; then | |
| echo "๐ Copying environment variables..." | |
| cp "/home/ubuntu/env/${ENV_FILE}" "${DEPLOY_DIR}/.env.production" | |
| fi | |
| # Docker ์ด๋ฏธ์ง ๋น๋ | |
| echo "๐จ Building Docker image..." | |
| DOCKER_BUILDKIT=1 docker compose build ${CONTAINER_NAME} | |
| # ์ปจํ ์ด๋ ๊ต์ฒด (๋ฌด์ค๋จ ๋ฐฐํฌ) | |
| echo "๐ Updating container..." | |
| docker compose up -d --no-deps ${CONTAINER_NAME} | |
| # ๋ค๋ฅธ ์ปจํ ์ด๋ ์ฌ์์ | |
| echo "๐ Restarting other container..." | |
| docker compose up -d ${OTHER_CONTAINER} | |
| # ํฌ์ค์ฒดํฌ | |
| echo "๐ฅ Health check..." | |
| sleep 10 | |
| if docker ps | grep -q ${CONTAINER_NAME}; then | |
| echo "โ Container is running" | |
| docker logs --tail 20 ${CONTAINER_NAME} | |
| else | |
| echo "โ Container failed to start" | |
| docker logs ${CONTAINER_NAME} | |
| exit 1 | |
| fi | |
| echo "โ Deployment completed successfully!" |