CD - Deploy to Server #71
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: CD - Deploy to Server | |
| on: | |
| workflow_run: | |
| workflows: ["CI - Build and Test"] | |
| types: [completed] | |
| branches: [ develop, main ] | |
| env: | |
| DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-fork | |
| jobs: | |
| docker-build: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Download JAR from CI | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: tech-fork-jar | |
| path: build/libs/ | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/arm64 | |
| push: true | |
| tags: | | |
| ${{ env.DOCKER_IMAGE }}:${{ github.event.workflow_run.head_branch }} | |
| ${{ env.DOCKER_IMAGE }}:${{ github.event.workflow_run.head_branch }}-${{ github.event.workflow_run.head_sha }} | |
| deploy: | |
| needs: [docker-build] | |
| runs-on: ubuntu-latest | |
| env: | |
| BRANCH: ${{ github.event.workflow_run.head_branch }} | |
| SPRING_PROFILES_ACTIVE: dev | |
| DB_URL: ${{ secrets.DB_URL }} | |
| DB_USERNAME: ${{ secrets.DB_USERNAME }} | |
| DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
| REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }} | |
| DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} | |
| ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} | |
| OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Copy deployment files to server | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: "docker-compose.yml,deploy.sh" | |
| target: "~/deploy/" | |
| - name: Deploy with docker-compose | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_PASSWORD,REDIS_PASSWORD,DISCORD_WEBHOOK_URL,ANTHROPIC_API_KEY,OPENAI_API_KEY | |
| script: | | |
| cd ~/deploy | |
| chmod +x deploy.sh | |
| ./deploy.sh | |
| - name: Health check | |
| run: | | |
| echo "🚀 Starting health check..." | |
| for i in {1..18}; do | |
| response=$(curl -s -o /dev/null -w "%{http_code}" https://techfork.shop/actuator/health || echo "000") | |
| if [ "$response" = "200" ]; then | |
| echo "✅ (Attempt $i/18) Health check successful!" | |
| exit 0 | |
| fi | |
| echo "🟡 (Attempt $i/18) Health check failed with status $response. Retrying in 5 seconds..." | |
| sleep 5 | |
| done | |
| echo "❌ Health check failed after 90 seconds." | |
| exit 1 |