Update docker-build-deploy.yml #3
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: Build and Push Docker Image to GHCR | |
| on: | |
| workflow_dispatch: # 手动触发 | |
| push: | |
| branches: [ main, master ] | |
| pull_request: | |
| branches: [ main, master ] | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Get short SHA | |
| id: vars | |
| run: echo "short_sha=${GITHUB_SHA::8}" >> $GITHUB_OUTPUT | |
| - name: Log in to GHCR with GITHUB_TOKEN | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push frontend image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./frontend | |
| file: ./frontend/Dockerfile | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository_owner }}/kb-frontend:latest | |
| ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ github.sha }} | |
| ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ steps.vars.outputs.short_sha }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| platforms: linux/amd64 | |
| - name: Build and push backend image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./backend | |
| file: ./backend/Dockerfile | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository_owner }}/kb-backend:latest | |
| ghcr.io/${{ github.repository_owner }}/kb-backend:${{ github.sha }} | |
| ghcr.io/${{ github.repository_owner }}/kb-backend:${{ steps.vars.outputs.short_sha }} | |
| build-args: | | |
| # 构建时传递环境变量 | |
| DATABASE_URL=${{ secrets.DATABASE_URL || 'sqlite:///./kb_upload.db' }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| platforms: linux/amd64 | |
| - name: Build summary | |
| run: | | |
| echo "✅ 容器构建完成!" | |
| echo "" | |
| echo "前端镜像标签:" | |
| echo " - ghcr.io/${{ github.repository_owner }}/kb-frontend:latest" | |
| echo " - ghcr.io/${{ github.repository_owner }}/kb-frontend:${{ steps.vars.outputs.short_sha }}" | |
| echo "" | |
| echo "后端镜像标签:" | |
| echo " - ghcr.io/${{ github.repository_owner }}/kb-backend:latest" | |
| echo " - ghcr.io/${{ github.repository_owner }}/kb-backend:${{ steps.vars.outputs.short_sha }}" |