|
| 1 | +name: Build and Push to Docker Hub |
| 2 | + |
| 3 | +# 在测试通过后自动构建并推送镜像到 Docker Hub |
| 4 | + |
| 5 | +# 触发条件: |
| 6 | +# 1. CI workflow (Full Test Suite) 成功完成后(主要触发方式) |
| 7 | +# 2. 手动触发(workflow_dispatch) |
| 8 | +on: |
| 9 | + workflow_run: |
| 10 | + workflows: ["Full Test Suite"] |
| 11 | + types: |
| 12 | + - completed |
| 13 | + branches: |
| 14 | + - main |
| 15 | + workflow_dispatch: # 允许手动触发 |
| 16 | + |
| 17 | +# Cancel old builds on the same branch |
| 18 | +concurrency: |
| 19 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + build-and-push: |
| 24 | + name: Build and Push Docker Images |
| 25 | + runs-on: ubuntu-latest |
| 26 | + # 如果是 workflow_run 触发,只在前一个 workflow 成功时运行 |
| 27 | + # 手动触发时总是运行 |
| 28 | + if: | |
| 29 | + github.event_name == 'workflow_dispatch' || |
| 30 | + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout code |
| 34 | + uses: actions/checkout@v4 |
| 35 | + with: |
| 36 | + # 如果是 workflow_run 触发,使用原始 workflow 的 commit SHA |
| 37 | + ref: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 38 | + fetch-depth: 0 |
| 39 | + |
| 40 | + - name: Set up Docker Buildx |
| 41 | + uses: docker/setup-buildx-action@v3 |
| 42 | + |
| 43 | + - name: Login to Docker Hub |
| 44 | + uses: docker/login-action@v3 |
| 45 | + with: |
| 46 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 47 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 48 | + |
| 49 | + - name: Extract metadata for backend |
| 50 | + id: meta-backend |
| 51 | + uses: docker/metadata-action@v5 |
| 52 | + with: |
| 53 | + images: ${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-backend |
| 54 | + tags: | |
| 55 | + type=ref,event=branch |
| 56 | + type=semver,pattern={{version}} |
| 57 | + type=semver,pattern={{major}}.{{minor}} |
| 58 | + type=sha |
| 59 | + type=raw,value=latest,enable={{is_default_branch}} |
| 60 | + |
| 61 | + - name: Extract metadata for frontend |
| 62 | + id: meta-frontend |
| 63 | + uses: docker/metadata-action@v5 |
| 64 | + with: |
| 65 | + images: ${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-frontend |
| 66 | + tags: | |
| 67 | + type=ref,event=branch |
| 68 | + type=semver,pattern={{version}} |
| 69 | + type=semver,pattern={{major}}.{{minor}} |
| 70 | + type=sha |
| 71 | + type=raw,value=latest,enable={{is_default_branch}} |
| 72 | + |
| 73 | + - name: Build and push backend image |
| 74 | + uses: docker/build-push-action@v5 |
| 75 | + with: |
| 76 | + context: . |
| 77 | + file: ./backend/Dockerfile |
| 78 | + push: true |
| 79 | + tags: ${{ steps.meta-backend.outputs.tags }} |
| 80 | + labels: ${{ steps.meta-backend.outputs.labels }} |
| 81 | + cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-backend:buildcache |
| 82 | + cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-backend:buildcache,mode=max |
| 83 | + build-args: | |
| 84 | + DOCKER_REGISTRY=${{ secrets.DOCKER_REGISTRY }} |
| 85 | + GHCR_REGISTRY=${{ secrets.GHCR_REGISTRY || 'ghcr.io/' }} |
| 86 | + APT_MIRROR=${{ secrets.APT_MIRROR }} |
| 87 | + PYPI_INDEX_URL=${{ secrets.PYPI_INDEX_URL }} |
| 88 | + |
| 89 | + - name: Build and push frontend image |
| 90 | + uses: docker/build-push-action@v5 |
| 91 | + with: |
| 92 | + context: . |
| 93 | + file: ./frontend/Dockerfile |
| 94 | + push: true |
| 95 | + tags: ${{ steps.meta-frontend.outputs.tags }} |
| 96 | + labels: ${{ steps.meta-frontend.outputs.labels }} |
| 97 | + cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-frontend:buildcache |
| 98 | + cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/banana-slides-frontend:buildcache,mode=max |
| 99 | + build-args: | |
| 100 | + DOCKER_BUILDKIT=1 |
| 101 | + VITE_API_BASE_URL=${{ secrets.VITE_API_BASE_URL }} |
| 102 | + DOCKER_REGISTRY=${{ secrets.DOCKER_REGISTRY }} |
| 103 | + NPM_REGISTRY=${{ secrets.NPM_REGISTRY }} |
| 104 | + |
| 105 | + - name: Image digest |
| 106 | + run: | |
| 107 | + echo "Backend image: ${{ steps.meta-backend.outputs.tags }}" |
| 108 | + echo "Frontend image: ${{ steps.meta-frontend.outputs.tags }}" |
| 109 | + echo "✅ Images pushed to Docker Hub successfully!" |
| 110 | +
|
0 commit comments