|
| 1 | +name: Build & Push Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - dev |
| 8 | + tags: |
| 9 | + - 'v*.*.*' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +env: |
| 13 | + REGISTRY: ghcr.io |
| 14 | + IMAGE_PREFIX: ghcr.io/${{ github.repository_owner }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + # ============================================================ |
| 18 | + # Backend: FastAPI Server |
| 19 | + # ============================================================ |
| 20 | + build-backend-server: |
| 21 | + name: Build Backend (Server) |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + packages: write |
| 26 | + |
| 27 | + steps: |
| 28 | + - name: Checkout |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Set up Docker Buildx |
| 32 | + uses: docker/setup-buildx-action@v3 |
| 33 | + |
| 34 | + - name: Login to GHCR |
| 35 | + uses: docker/login-action@v3 |
| 36 | + with: |
| 37 | + registry: ${{ env.REGISTRY }} |
| 38 | + username: ${{ github.actor }} |
| 39 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 40 | + |
| 41 | + - name: Docker metadata (server) |
| 42 | + id: meta |
| 43 | + uses: docker/metadata-action@v5 |
| 44 | + with: |
| 45 | + images: ${{ env.IMAGE_PREFIX }}/wemcp-server |
| 46 | + tags: | |
| 47 | + type=ref,event=branch |
| 48 | + type=semver,pattern={{version}} |
| 49 | + type=semver,pattern={{major}}.{{minor}} |
| 50 | + type=sha,prefix=sha-,format=short |
| 51 | +
|
| 52 | + - name: Build and push (server) |
| 53 | + uses: docker/build-push-action@v6 |
| 54 | + with: |
| 55 | + context: ./backend |
| 56 | + file: ./backend/Dockerfile |
| 57 | + build-args: | |
| 58 | + SERVER_TYPE=fastapi_server |
| 59 | + push: true |
| 60 | + tags: ${{ steps.meta.outputs.tags }} |
| 61 | + labels: ${{ steps.meta.outputs.labels }} |
| 62 | + cache-from: type=gha,scope=backend-server |
| 63 | + cache-to: type=gha,mode=max,scope=backend-server |
| 64 | + |
| 65 | + # ============================================================ |
| 66 | + # Backend: Celery Worker |
| 67 | + # ============================================================ |
| 68 | + build-backend-worker: |
| 69 | + name: Build Backend (Worker) |
| 70 | + runs-on: ubuntu-latest |
| 71 | + permissions: |
| 72 | + contents: read |
| 73 | + packages: write |
| 74 | + |
| 75 | + steps: |
| 76 | + - name: Checkout |
| 77 | + uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Set up Docker Buildx |
| 80 | + uses: docker/setup-buildx-action@v3 |
| 81 | + |
| 82 | + - name: Login to GHCR |
| 83 | + uses: docker/login-action@v3 |
| 84 | + with: |
| 85 | + registry: ${{ env.REGISTRY }} |
| 86 | + username: ${{ github.actor }} |
| 87 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + |
| 89 | + - name: Docker metadata (worker) |
| 90 | + id: meta |
| 91 | + uses: docker/metadata-action@v5 |
| 92 | + with: |
| 93 | + images: ${{ env.IMAGE_PREFIX }}/wemcp-celery |
| 94 | + tags: | |
| 95 | + type=ref,event=branch |
| 96 | + type=semver,pattern={{version}} |
| 97 | + type=semver,pattern={{major}}.{{minor}} |
| 98 | + type=sha,prefix=sha-,format=short |
| 99 | +
|
| 100 | + - name: Build and push (worker) |
| 101 | + uses: docker/build-push-action@v6 |
| 102 | + with: |
| 103 | + context: ./backend |
| 104 | + file: ./backend/Dockerfile |
| 105 | + build-args: | |
| 106 | + SERVER_TYPE=celery |
| 107 | + push: true |
| 108 | + tags: ${{ steps.meta.outputs.tags }} |
| 109 | + labels: ${{ steps.meta.outputs.labels }} |
| 110 | + cache-from: type=gha,scope=backend-worker |
| 111 | + cache-to: type=gha,mode=max,scope=backend-worker |
| 112 | + |
| 113 | + # ============================================================ |
| 114 | + # Frontend: Next.js |
| 115 | + # ============================================================ |
| 116 | + build-frontend: |
| 117 | + name: Build Frontend |
| 118 | + runs-on: ubuntu-latest |
| 119 | + permissions: |
| 120 | + contents: read |
| 121 | + packages: write |
| 122 | + |
| 123 | + steps: |
| 124 | + - name: Checkout |
| 125 | + uses: actions/checkout@v4 |
| 126 | + |
| 127 | + - name: Set up Docker Buildx |
| 128 | + uses: docker/setup-buildx-action@v3 |
| 129 | + |
| 130 | + - name: Login to GHCR |
| 131 | + uses: docker/login-action@v3 |
| 132 | + with: |
| 133 | + registry: ${{ env.REGISTRY }} |
| 134 | + username: ${{ github.actor }} |
| 135 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 136 | + |
| 137 | + # 前端 Dockerfile 会 COPY deploy/.env.production,需要提前写入 |
| 138 | + - name: Write frontend .env.production |
| 139 | + run: | |
| 140 | + mkdir -p frontend/deploy |
| 141 | + echo "${{ secrets.FRONTEND_ENV_PRODUCTION }}" > frontend/deploy/.env.production |
| 142 | +
|
| 143 | + - name: Docker metadata (frontend) |
| 144 | + id: meta |
| 145 | + uses: docker/metadata-action@v5 |
| 146 | + with: |
| 147 | + images: ${{ env.IMAGE_PREFIX }}/wemcp-frontend |
| 148 | + tags: | |
| 149 | + type=ref,event=branch |
| 150 | + type=semver,pattern={{version}} |
| 151 | + type=semver,pattern={{major}}.{{minor}} |
| 152 | + type=sha,prefix=sha-,format=short |
| 153 | +
|
| 154 | + - name: Build and push (frontend) |
| 155 | + uses: docker/build-push-action@v6 |
| 156 | + with: |
| 157 | + context: ./frontend |
| 158 | + file: ./frontend/Dockerfile |
| 159 | + build-args: | |
| 160 | + COS_SECRET_ID=${{ secrets.COS_SECRET_ID }} |
| 161 | + COS_SECRET_KEY=${{ secrets.COS_SECRET_KEY }} |
| 162 | + COS_BUCKET=${{ secrets.COS_BUCKET }} |
| 163 | + COS_REGION=${{ secrets.COS_REGION }} |
| 164 | + push: true |
| 165 | + tags: ${{ steps.meta.outputs.tags }} |
| 166 | + labels: ${{ steps.meta.outputs.labels }} |
| 167 | + cache-from: type=gha,scope=frontend |
| 168 | + cache-to: type=gha,mode=max,scope=frontend |
0 commit comments