Finders CD (Deploy to Prod) #19
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: Finders CD (Deploy to Prod) | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| env: | |
| DOCKER_IMAGE: ${{ secrets.DOCKER_USERNAME }}/finders-api | |
| CONTAINER_NAME: finders-api | |
| permissions: | |
| contents: read | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: Build without Test | |
| # CI์์ ์ด๋ฏธ ํ ์คํธ๋ฅผ ํต๊ณผํ๋ค๊ณ ๋ฏฟ๊ณ , ๋ฐฐํฌ ์๋๋ฅผ ์ํด ํ ์คํธ๋ ๊ฑด๋๋๋๋ค. | |
| run: ./gradlew build -x test | |
| # 4. Docker Hub ๋ก๊ทธ์ธ | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| # 5. Docker ์ด๋ฏธ์ง ๋น๋ ๋ฐ ํธ์ | |
| - name: Build and Push | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ env.DOCKER_IMAGE }}:latest | |
| # 6. docker-compose.prod.yml ํ์ผ์ ์๋ฒ๋ก ์ ์ก (SCP) | |
| - name: Copy Docker Compose file to Server | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ secrets.GCE_HOST }} | |
| username: ${{ secrets.GCE_USER }} | |
| key: ${{ secrets.GCE_SSH_KEY }} | |
| port: 22 | |
| source: "docker-compose.prod.yml" | |
| target: "/home/${{ secrets.GCE_USER }}/app" | |
| strip_components: 0 | |
| # 7. GCE ์๋ฒ์ ๋ฐฐํฌ ๋ฐ ๊ฒ์ฆ (Health Check ์ถ๊ฐ๋จ) | |
| - name: Deploy to GCE | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.GCE_HOST }} | |
| username: ${{ secrets.GCE_USER }} | |
| key: ${{ secrets.GCE_SSH_KEY }} | |
| port: 22 | |
| script: | | |
| # 1. ์ฑ ๋๋ ํ ๋ฆฌ ์ด๋ ๋ฐ ํ๊ฒฝ๋ณ์ ์ค์ | |
| cd ~/app | |
| echo "${{ secrets.ENV_PROD }}" > .env.prod | |
| # 2. ์ต์ ์ด๋ฏธ์ง Pull | |
| sudo docker compose -f docker-compose.prod.yml pull | |
| # 3. ๊ธฐ์กด ์ปจํ ์ด๋ ๋ด๋ฆฌ๊ณ ์๋ก ์์ | |
| sudo docker compose -f docker-compose.prod.yml down | |
| sudo docker compose -f docker-compose.prod.yml up -d | |
| # 4. ๋ฐฐํฌ ๊ฒ์ฆ (Health Check) ๋ก์ง | |
| echo "๋ฐฐํฌ ํ Health Check ์์..." | |
| # ์ต๋ 60์ด(5์ด*12ํ) ๋๊ธฐ | |
| for i in {1..12}; do | |
| RESPONSE=$(curl -s http://localhost:8080/api/actuator/health || true) | |
| if [[ "$RESPONSE" == *"UP"* ]]; then | |
| echo "โ ์๋น์ค ์ ์ ์คํ ํ์ธ! (Attempt $i)" | |
| sudo docker image prune -f # ๋ถํ์ํ ์ด๋ฏธ์ง ์ ๋ฆฌ | |
| exit 0 # ์ฑ๊ณต์ผ๋ก ์ข ๋ฃ | |
| fi | |
| echo "์๋น์ค ์์ ๋๊ธฐ ์ค... ($i/12)" | |
| sleep 5 | |
| done | |
| # 5. ์คํจ ์ ์ฒ๋ฆฌ | |
| echo "โ ๋ฐฐํฌ ์คํจ: ์๋น์ค๊ฐ ์ ํ ์๊ฐ ๋ด์ ๋จ์ง ์์์ต๋๋ค." | |
| sudo docker compose -f docker-compose.prod.yml logs --tail=100 | |
| exit 1 # GitHub Actions๋ฅผ '์คํจ(Red)'๋ก ์ฒ๋ฆฌ |