From b399b573c14892792f6ac37932781aae931b2c2f Mon Sep 17 00:00:00 2001 From: ytqh Date: Thu, 3 Jul 2025 19:19:49 +0800 Subject: [PATCH] Fix deploy-backend workflow: separate build and deploy phases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Build & push images: Only on PR creation/updates (not on merge) - Deploy to production: Only on merge to main branch or manual trigger - Improved workflow clarity with emojis and better step descriptions - Added deployment progress logging for better visibility - Fixed workflow logic to prevent unnecessary builds on merge This ensures: ✅ PR creates/updates → Build and push Docker images ✅ Merge to main → Deploy using pre-built images ✅ Manual deployment → Deploy latest images 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .github/workflows/deploy-backend.yml | 44 +++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/.github/workflows/deploy-backend.yml b/.github/workflows/deploy-backend.yml index 8e4f12c5ca..cfe08b2db7 100644 --- a/.github/workflows/deploy-backend.yml +++ b/.github/workflows/deploy-backend.yml @@ -1,42 +1,55 @@ name: Deploy Backend API on: + # Build and push on PR creation/update pull_request: - types: [closed] branches: - main - master + types: [opened, synchronize, reopened] + + # Deploy on merge to main + push: + branches: + - main + - master + + # Allow manual deployment workflow_dispatch: jobs: build-and-push: - # Only run if PR was merged or manually triggered - if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true) + name: 🔨 Build & Push Images (PR Only) + # Only run on PR events (not on push to main) + if: github.event_name == 'pull_request' runs-on: ubuntu-latest steps: - - name: Checkout code + - name: 📥 Checkout code uses: actions/checkout@v4 - - name: Set up Docker Buildx + - name: 🔧 Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Login to Docker Hub + - name: 🔐 Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Build and push API image using Makefile + - name: 🏗️ Build and push API image run: | + echo "Building and pushing API image for PR #${{ github.event.number }}" make build-push-api deploy: - needs: build-and-push + name: 🚀 Deploy to Production (Main Branch Only) + # Only run on push to main (merge) or manual trigger + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: - - name: Deploy to Production Server + - name: 🚀 Deploy to Production Server uses: appleboy/ssh-action@v1.0.3 with: host: qingsu.chat @@ -44,12 +57,23 @@ jobs: key: ${{ secrets.SSH_PRIVATE_KEY }} port: 22 script: | + echo "🚀 Starting deployment to production server..." cd /root/projects/jim-dify + + echo "📦 Updating source code..." git fetch origin git reset --hard origin/$(git branch --show-current) + + echo "🐳 Updating Docker images..." cd docker export HTTP_PROXY=http://host.docker.internal:1081 export HTTPS_PROXY=http://host.docker.internal:1081 export NO_PROXY=weaviate,sandbox,plugin_daemon,localhost,127.0.0.1 + + # Pull latest images (built from the merged PR) docker compose pull - docker compose up -d --force-recreate nginx web api worker scheduler \ No newline at end of file + + echo "🔄 Restarting services..." + docker compose up -d --force-recreate nginx web api worker scheduler + + echo "✅ Deployment completed successfully!" \ No newline at end of file