|
| 1 | +name: Build & Publish Docker image (GHCR) |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + tags: [ 'v*.*.*' ] # 推 tag 时也会发版 |
| 7 | + workflow_dispatch: # 手动触发 |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-push: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + packages: write # 关键:允许推送到 GHCR |
| 15 | + attestations: write # 便于后续生成 SBOM/证明 |
| 16 | + id-token: write |
| 17 | + |
| 18 | + env: |
| 19 | + REGISTRY: ghcr.io |
| 20 | + IMAGE_NAME: ${{ github.repository }} |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up QEMU (multi-arch) |
| 27 | + uses: docker/setup-qemu-action@v3 |
| 28 | + |
| 29 | + - name: Set up Docker Buildx |
| 30 | + uses: docker/setup-buildx-action@v3 |
| 31 | + |
| 32 | + - name: Log in to GHCR |
| 33 | + uses: docker/login-action@v3 |
| 34 | + with: |
| 35 | + registry: ghcr.io |
| 36 | + username: ${{ github.repository_owner }} |
| 37 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + |
| 39 | + - name: Extract Docker metadata (tags, labels) |
| 40 | + id: meta |
| 41 | + uses: docker/metadata-action@v5 |
| 42 | + with: |
| 43 | + images: ghcr.io/${{ github.repository }} |
| 44 | + # 约定:main 分支 -> :latest;同时打上 sha 短标签;打 tag 时 -> 复用 tag 作为版本 |
| 45 | + tags: | |
| 46 | + type=raw,value=latest,enable={{is_default_branch}} |
| 47 | + type=ref,event=tag |
| 48 | + type=sha |
| 49 | +
|
| 50 | + - name: Build and push |
| 51 | + uses: docker/build-push-action@v6 |
| 52 | + with: |
| 53 | + context: . |
| 54 | + file: ./Dockerfile |
| 55 | + push: true |
| 56 | + platforms: linux/amd64,linux/arm64 # 同时构建 x64 + arm64(Apple M 芯片友好) |
| 57 | + tags: ${{ steps.meta.outputs.tags }} |
| 58 | + labels: ${{ steps.meta.outputs.labels }} |
| 59 | + cache-from: type=gha |
| 60 | + cache-to: type=gha,mode=max |
0 commit comments