🔄 ci: add retry mechanism for docker pull during deployment #24
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 Dev Server | |
| on: | |
| push: | |
| branches: [main] | |
| workflow_dispatch: | |
| jobs: | |
| checks: | |
| uses: ./.github/workflows/python-checks.yml | |
| build-and-push: | |
| needs: checks | |
| runs-on: ubuntu-latest | |
| 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.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| hendisantika/jvmid-bot:latest | |
| hendisantika/jvmid-bot:${{ github.sha }} | |
| hendisantika/jvmid-bot:${{ github.run_number }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up SSH key | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEV_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| echo "${{ secrets.DEV_SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts | |
| chmod 644 ~/.ssh/known_hosts | |
| - name: Create app directory on server | |
| run: | | |
| ssh deployer@103.31.204.189 'mkdir -p ~/jvmid-bot/data' | |
| - name: Write .env file to server | |
| run: | | |
| ssh deployer@103.31.204.189 "echo '${{ vars.DEV_ENV_FILE }}' > ~/jvmid-bot/.env" | |
| - name: Write groups.json to server | |
| run: | | |
| echo '${{ vars.DEV_GROUPS_JSON }}' > /tmp/groups.json | |
| scp /tmp/groups.json deployer@103.31.204.189:~/jvmid-bot/groups.json | |
| rm /tmp/groups.json | |
| - name: Copy docker-compose.prod.yml to server | |
| run: | | |
| scp docker-compose.prod.yml deployer@103.31.204.189:~/jvmid-bot/docker-compose.prod.yml | |
| - name: Deploy on server | |
| run: | | |
| ssh deployer@103.31.204.189 ' | |
| cd ~/jvmid-bot | |
| export DOCKER_UID=$(id -u) DOCKER_GID=$(id -g) | |
| # Pull image with retry (up to 5 attempts, 15s between retries) | |
| MAX_RETRIES=5 | |
| for i in $(seq 1 $MAX_RETRIES); do | |
| echo "Pull attempt $i/$MAX_RETRIES..." | |
| if IMAGE_TAG=${{ github.run_number }} docker compose -f docker-compose.prod.yml pull 2>&1; then | |
| echo "Pull succeeded" | |
| break | |
| fi | |
| if [ "$i" -eq "$MAX_RETRIES" ]; then | |
| echo "Pull failed after $MAX_RETRIES attempts" | |
| exit 1 | |
| fi | |
| echo "Pull failed, retrying in 15s..." | |
| sleep 15 | |
| done | |
| IMAGE_TAG=${{ github.run_number }} docker compose -f docker-compose.prod.yml up -d | |
| docker image prune -f | |
| echo "Deployment complete (tag: ${{ github.run_number }}). Running containers:" | |
| docker ps --filter "name=jvmid-bot" | |
| ' |