|
1 |
| -name: Docker Build and Publish |
| 1 | +name: Build and Push Docker Images |
2 | 2 |
|
3 | 3 | on:
|
4 | 4 | push:
|
5 |
| - branches: [ "main" ] |
| 5 | + branches: |
| 6 | + - main # 当推送到 main 分支时触发 |
6 | 7 | workflow_dispatch: # 允许手动触发
|
7 | 8 |
|
8 | 9 | env:
|
9 | 10 | IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/deep-research-web
|
10 | 11 |
|
11 | 12 | jobs:
|
12 |
| - build: |
| 13 | + docker: |
| 14 | + name: Build Docker Image (${{ matrix.arch }}) |
| 15 | + runs-on: ${{ matrix.runner }} |
| 16 | + |
13 | 17 | strategy:
|
14 | 18 | matrix:
|
15 | 19 | include:
|
16 |
| - - platform: linux/amd64 |
17 |
| - runner: ubuntu-latest |
18 |
| - - platform: linux/arm64 |
19 |
| - runner: ubuntu-24.04-arm |
20 |
| - runs-on: ${{ matrix.runner }} |
21 |
| - permissions: |
22 |
| - contents: read |
23 |
| - packages: write |
| 20 | + - arch: amd64 |
| 21 | + runner: ubuntu-22.04 |
| 22 | + platform: linux/amd64 |
| 23 | + - arch: arm64 |
| 24 | + runner: ubuntu-22.04-arm |
| 25 | + platform: linux/arm64 |
24 | 26 |
|
25 | 27 | steps:
|
26 | 28 | - name: Checkout repository
|
27 | 29 | uses: actions/checkout@v4
|
28 | 30 |
|
29 | 31 | - name: Set up Docker Buildx
|
30 | 32 | uses: docker/setup-buildx-action@v3
|
31 |
| - with: |
32 |
| - platforms: ${{ matrix.platform }} |
33 | 33 |
|
34 |
| - - name: Log in to Docker Hub |
| 34 | + - name: Login to Docker Hub |
35 | 35 | uses: docker/login-action@v3
|
36 | 36 | with:
|
37 | 37 | username: ${{ secrets.DOCKERHUB_USERNAME }}
|
38 | 38 | password: ${{ secrets.DOCKERHUB_TOKEN }}
|
39 | 39 |
|
40 |
| - - name: Extract metadata (tags, labels) for Docker |
| 40 | + - name: Docker metadata |
41 | 41 | id: meta
|
42 | 42 | uses: docker/metadata-action@v5
|
43 | 43 | with:
|
44 | 44 | images: ${{ env.IMAGE_NAME }}
|
45 | 45 | tags: |
|
46 |
| - type=raw,value=latest,enable={{is_default_branch}} |
47 |
| - type=sha,prefix={{branch}}- |
48 | 46 | type=ref,event=branch
|
49 |
| - type=ref,event=tag |
| 47 | + type=ref,event=pr |
| 48 | + type=semver,pattern={{version}} |
| 49 | + type=semver,pattern={{major}}.{{minor}} |
| 50 | + type=sha,format=long |
| 51 | + type=raw,value=latest,enable={{is_default_branch}} |
50 | 52 |
|
51 |
| - - name: Build and push Docker image |
| 53 | + - name: Build and push by digest |
| 54 | + id: build |
52 | 55 | uses: docker/build-push-action@v5
|
53 | 56 | with:
|
54 | 57 | context: .
|
55 |
| - push: true |
56 | 58 | platforms: ${{ matrix.platform }}
|
57 |
| - tags: ${{ steps.meta.outputs.tags }} |
58 | 59 | labels: ${{ steps.meta.outputs.labels }}
|
59 |
| - cache-from: | |
60 |
| - type=gha,scope=${{ matrix.platform }} |
61 |
| - cache-to: | |
62 |
| - type=gha,mode=max,scope=${{ matrix.platform }} |
63 |
| - target: runner # 只构建到最终阶段 |
| 60 | + outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true |
| 61 | + cache-from: type=gha |
| 62 | + cache-to: type=gha,mode=max |
| 63 | + |
| 64 | + - name: Export digest |
| 65 | + run: | |
| 66 | + mkdir -p /tmp/digests |
| 67 | + digest="${{ steps.build.outputs.digest }}" |
| 68 | + touch "/tmp/digests/${digest#sha256:}" |
| 69 | +
|
| 70 | + - name: Upload digest |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: digests-${{ matrix.arch }} |
| 74 | + path: /tmp/digests/* |
| 75 | + if-no-files-found: error |
| 76 | + retention-days: 1 |
| 77 | + |
| 78 | + merge: |
| 79 | + name: Merge and Push Multi-arch Image |
| 80 | + needs: [docker] |
| 81 | + runs-on: ubuntu-22.04 |
| 82 | + |
| 83 | + steps: |
| 84 | + - name: Download digests |
| 85 | + uses: actions/download-artifact@v4 |
| 86 | + with: |
| 87 | + pattern: digests-* |
| 88 | + path: /tmp/digests |
| 89 | + merge-multiple: true |
| 90 | + |
| 91 | + - name: Set up Docker Buildx |
| 92 | + uses: docker/setup-buildx-action@v3 |
| 93 | + |
| 94 | + - name: Login to Docker Hub |
| 95 | + uses: docker/login-action@v3 |
| 96 | + with: |
| 97 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 98 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 99 | + |
| 100 | + - name: Docker metadata |
| 101 | + id: meta |
| 102 | + uses: docker/metadata-action@v5 |
| 103 | + with: |
| 104 | + images: ${{ env.IMAGE_NAME }} |
| 105 | + tags: | |
| 106 | + type=ref,event=branch |
| 107 | + type=ref,event=pr |
| 108 | + type=semver,pattern={{version}} |
| 109 | + type=semver,pattern={{major}}.{{minor}} |
| 110 | + type=sha,format=long |
| 111 | + type=raw,value=latest,enable={{is_default_branch}} |
| 112 | +
|
| 113 | + - name: Create manifest list and push |
| 114 | + working-directory: /tmp/digests |
| 115 | + run: | |
| 116 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< '${{ steps.meta.outputs.json }}') \ |
| 117 | + $(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *) |
0 commit comments