Fix deploy-backend workflow: separate build and deploy phases

- 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 <noreply@anthropic.com>
pull/21891/head
ytqh 11 months ago
parent 816ea24571
commit b399b573c1

@ -1,42 +1,55 @@
name: Deploy Backend API name: Deploy Backend API
on: on:
# Build and push on PR creation/update
pull_request: pull_request:
types: [closed]
branches: branches:
- main - main
- master - master
types: [opened, synchronize, reopened]
# Deploy on merge to main
push:
branches:
- main
- master
# Allow manual deployment
workflow_dispatch: workflow_dispatch:
jobs: jobs:
build-and-push: build-and-push:
# Only run if PR was merged or manually triggered name: 🔨 Build & Push Images (PR Only)
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true) # Only run on PR events (not on push to main)
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: 📥 Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Docker Buildx - name: 🔧 Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub - name: 🔐 Login to Docker Hub
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
username: ${{ secrets.DOCKER_USERNAME }} username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }} password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push API image using Makefile - name: 🏗️ Build and push API image
run: | run: |
echo "Building and pushing API image for PR #${{ github.event.number }}"
make build-push-api make build-push-api
deploy: 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 runs-on: ubuntu-latest
steps: steps:
- name: Deploy to Production Server - name: 🚀 Deploy to Production Server
uses: appleboy/ssh-action@v1.0.3 uses: appleboy/ssh-action@v1.0.3
with: with:
host: qingsu.chat host: qingsu.chat
@ -44,12 +57,23 @@ jobs:
key: ${{ secrets.SSH_PRIVATE_KEY }} key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22 port: 22
script: | script: |
echo "🚀 Starting deployment to production server..."
cd /root/projects/jim-dify cd /root/projects/jim-dify
echo "📦 Updating source code..."
git fetch origin git fetch origin
git reset --hard origin/$(git branch --show-current) git reset --hard origin/$(git branch --show-current)
echo "🐳 Updating Docker images..."
cd docker cd docker
export HTTP_PROXY=http://host.docker.internal:1081 export HTTP_PROXY=http://host.docker.internal:1081
export HTTPS_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 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 pull
echo "🔄 Restarting services..."
docker compose up -d --force-recreate nginx web api worker scheduler docker compose up -d --force-recreate nginx web api worker scheduler
echo "✅ Deployment completed successfully!"
Loading…
Cancel
Save