|
| 1 | +name: Docker Build and Push |
| 2 | + |
| 3 | +# 这个工作流使用未经 GitHub 认证的操作。 |
| 4 | +# 它们由第三方提供,受单独的服务条款、隐私政策和支持文档约束。 |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +env: |
| 13 | + # 使用 docker.io 作为 Docker Hub(如果为空) |
| 14 | + REGISTRY: ghcr.io |
| 15 | + # github.repository 格式为 <account>/<repo> |
| 16 | + IMAGE_NAME: ${{ github.repository }} |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + build: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + |
| 26 | + permissions: |
| 27 | + contents: read |
| 28 | + packages: write |
| 29 | + id-token: write |
| 30 | + attestations: write |
| 31 | + |
| 32 | + |
| 33 | + steps: |
| 34 | + - name: Tune GitHub Hosted Runner Network |
| 35 | + uses: smorimoto/tune-github-hosted-runner-network@v1 |
| 36 | + |
| 37 | + - uses: actions/checkout@v5 |
| 38 | + |
| 39 | + - name: Set up Docker Buildx |
| 40 | + uses: docker/setup-buildx-action@v3 |
| 41 | + |
| 42 | + - name: Log in to Container Registry |
| 43 | + uses: docker/login-action@v3 |
| 44 | + with: |
| 45 | + registry: ${{ env.REGISTRY }} |
| 46 | + username: ${{ github.actor }} |
| 47 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + |
| 49 | + - name: Extract metadata |
| 50 | + id: meta |
| 51 | + uses: docker/metadata-action@v5 |
| 52 | + with: |
| 53 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 54 | + tags: | |
| 55 | + type=raw,value=latest,priority=500 |
| 56 | + type=raw,value=${{ github.run_number }},priority=400 |
| 57 | + type=ref,event=branch |
| 58 | + type=ref,event=pr |
| 59 | + type=sha,prefix={{branch}}- |
| 60 | +
|
| 61 | + - name: Build and push Docker image |
| 62 | + id: build-and-push |
| 63 | + uses: docker/build-push-action@v6 |
| 64 | + with: |
| 65 | + context: . |
| 66 | + pull: true |
| 67 | + push: true |
| 68 | + tags: ${{ steps.meta.outputs.tags }} |
| 69 | + labels: ${{ steps.meta.outputs.labels }} |
| 70 | + secrets: | |
| 71 | + GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} |
| 72 | + platforms: linux/amd64, linux/arm64 |
| 73 | + cache-from: type=gha |
| 74 | + cache-to: type=gha,mode=max |
| 75 | + |
| 76 | + - name: Attest Build Provenance |
| 77 | + uses: actions/attest-build-provenance@v3 |
| 78 | + with: |
| 79 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 80 | + subject-digest: ${{ steps.build-and-push.outputs.digest }} |
| 81 | + push-to-registry: true |
| 82 | + |
| 83 | + cleanup: |
| 84 | + runs-on: ubuntu-latest |
| 85 | + needs: [build] |
| 86 | + if: always() |
| 87 | + |
| 88 | + permissions: |
| 89 | + packages: write |
| 90 | + |
| 91 | + steps: |
| 92 | + - name: Cleanup old packages |
| 93 | + uses: dataaxiom/ghcr-cleanup-action@v1 |
| 94 | + with: |
| 95 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + package-name: ${{ github.repository }} |
| 97 | + keep-last: 10 |
0 commit comments