|
@@ -1,57 +1,116 @@
|
|
|
name: Build and Push Docker Image
|
|
name: Build and Push Docker Image
|
|
|
|
|
|
|
|
-
|
|
|
|
|
on:
|
|
on:
|
|
|
push:
|
|
push:
|
|
|
- branches: [ master, main ]
|
|
|
|
|
|
|
+ branches: [master, main]
|
|
|
|
|
+ # 仅当构建相关文件变更时触发,避免无意义排队
|
|
|
|
|
+ paths:
|
|
|
|
|
+ - "backend/**"
|
|
|
|
|
+ - "frontend/**"
|
|
|
|
|
+ - ".github/workflows/docker-build.yml"
|
|
|
|
|
+ - "docker-compose*.yml"
|
|
|
workflow_dispatch:
|
|
workflow_dispatch:
|
|
|
|
|
+ inputs:
|
|
|
|
|
+ build_backend:
|
|
|
|
|
+ description: "手动触发时是否构建后端"
|
|
|
|
|
+ required: false
|
|
|
|
|
+ default: true
|
|
|
|
|
+ type: boolean
|
|
|
|
|
+ build_frontend:
|
|
|
|
|
+ description: "手动触发时是否构建前端"
|
|
|
|
|
+ required: false
|
|
|
|
|
+ default: true
|
|
|
|
|
+ type: boolean
|
|
|
|
|
+
|
|
|
|
|
+# 同一分支只保留最新一次,减少等待队列
|
|
|
|
|
+concurrency:
|
|
|
|
|
+ group: docker-build-${{ github.ref }}
|
|
|
|
|
+ cancel-in-progress: true
|
|
|
|
|
|
|
|
jobs:
|
|
jobs:
|
|
|
|
|
+ changes:
|
|
|
|
|
+ runs-on: ubuntu-latest
|
|
|
|
|
+ outputs:
|
|
|
|
|
+ backend: ${{ steps.filter.outputs.backend }}
|
|
|
|
|
+ frontend: ${{ steps.filter.outputs.frontend }}
|
|
|
|
|
+ steps:
|
|
|
|
|
+ - name: Checkout code
|
|
|
|
|
+ uses: actions/checkout@v4
|
|
|
|
|
+
|
|
|
|
|
+ - name: Detect changed paths
|
|
|
|
|
+ id: filter
|
|
|
|
|
+ uses: dorny/paths-filter@v3
|
|
|
|
|
+ with:
|
|
|
|
|
+ filters: |
|
|
|
|
|
+ backend:
|
|
|
|
|
+ - "backend/**"
|
|
|
|
|
+ - ".github/workflows/docker-build.yml"
|
|
|
|
|
+ - "docker-compose*.yml"
|
|
|
|
|
+ frontend:
|
|
|
|
|
+ - "frontend/**"
|
|
|
|
|
+ - ".github/workflows/docker-build.yml"
|
|
|
|
|
+ - "docker-compose*.yml"
|
|
|
|
|
+
|
|
|
build-backend:
|
|
build-backend:
|
|
|
runs-on: ubuntu-latest
|
|
runs-on: ubuntu-latest
|
|
|
|
|
+ needs: changes
|
|
|
|
|
+ if: |
|
|
|
|
|
+ github.event_name == 'workflow_dispatch' && inputs.build_backend == true ||
|
|
|
|
|
+ github.event_name != 'workflow_dispatch' && needs.changes.outputs.backend == 'true'
|
|
|
steps:
|
|
steps:
|
|
|
- name: Checkout code
|
|
- name: Checkout code
|
|
|
- uses: actions/checkout@v3
|
|
|
|
|
-
|
|
|
|
|
|
|
+ uses: actions/checkout@v4
|
|
|
|
|
+
|
|
|
- name: Set up Docker Buildx
|
|
- name: Set up Docker Buildx
|
|
|
- uses: docker/setup-buildx-action@v2
|
|
|
|
|
-
|
|
|
|
|
|
|
+ uses: docker/setup-buildx-action@v3
|
|
|
|
|
+
|
|
|
- name: Log in to Docker Hub
|
|
- name: Log in to Docker Hub
|
|
|
- uses: docker/login-action@v2
|
|
|
|
|
|
|
+ uses: docker/login-action@v3
|
|
|
with:
|
|
with:
|
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
- name: Build and push backend
|
|
- name: Build and push backend
|
|
|
- uses: docker/build-push-action@v4
|
|
|
|
|
|
|
+ uses: docker/build-push-action@v6
|
|
|
with:
|
|
with:
|
|
|
context: ./backend
|
|
context: ./backend
|
|
|
file: ./backend/Dockerfile
|
|
file: ./backend/Dockerfile
|
|
|
push: true
|
|
push: true
|
|
|
- tags: 537yaha/ai-cs-backend:latest
|
|
|
|
|
|
|
+ tags: |
|
|
|
|
|
+ 537yaha/ai-cs-backend:latest
|
|
|
platforms: linux/amd64,linux/arm64
|
|
platforms: linux/amd64,linux/arm64
|
|
|
|
|
+ # 使用 GitHub Actions 缓存构建层,显著减少重复构建时间
|
|
|
|
|
+ cache-from: type=gha
|
|
|
|
|
+ cache-to: type=gha,mode=max
|
|
|
|
|
|
|
|
build-frontend:
|
|
build-frontend:
|
|
|
runs-on: ubuntu-latest
|
|
runs-on: ubuntu-latest
|
|
|
|
|
+ needs: changes
|
|
|
|
|
+ if: |
|
|
|
|
|
+ github.event_name == 'workflow_dispatch' && inputs.build_frontend == true ||
|
|
|
|
|
+ github.event_name != 'workflow_dispatch' && needs.changes.outputs.frontend == 'true'
|
|
|
steps:
|
|
steps:
|
|
|
- name: Checkout code
|
|
- name: Checkout code
|
|
|
- uses: actions/checkout@v3
|
|
|
|
|
-
|
|
|
|
|
|
|
+ uses: actions/checkout@v4
|
|
|
|
|
+
|
|
|
- name: Set up Docker Buildx
|
|
- name: Set up Docker Buildx
|
|
|
- uses: docker/setup-buildx-action@v2
|
|
|
|
|
-
|
|
|
|
|
|
|
+ uses: docker/setup-buildx-action@v3
|
|
|
|
|
+
|
|
|
- name: Log in to Docker Hub
|
|
- name: Log in to Docker Hub
|
|
|
- uses: docker/login-action@v2
|
|
|
|
|
|
|
+ uses: docker/login-action@v3
|
|
|
with:
|
|
with:
|
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
- name: Build and push frontend
|
|
- name: Build and push frontend
|
|
|
- uses: docker/build-push-action@v4
|
|
|
|
|
|
|
+ uses: docker/build-push-action@v6
|
|
|
with:
|
|
with:
|
|
|
context: ./frontend
|
|
context: ./frontend
|
|
|
file: ./frontend/Dockerfile
|
|
file: ./frontend/Dockerfile
|
|
|
push: true
|
|
push: true
|
|
|
- tags: 537yaha/ai-cs-frontend:latest
|
|
|
|
|
|
|
+ tags: |
|
|
|
|
|
+ 537yaha/ai-cs-frontend:latest
|
|
|
platforms: linux/amd64,linux/arm64
|
|
platforms: linux/amd64,linux/arm64
|
|
|
- # 前端统一走同域 /api(由反向代理转发到后端),无需在构建时注入 localhost/端口
|
|
|
|
|
|
|
+ # 前端统一走同域 /api(由反向代理转发到后端),无需在构建时注入 localhost/端口
|
|
|
|
|
+ cache-from: type=gha
|
|
|
|
|
+ cache-to: type=gha,mode=max
|