docker-publish.yml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. name: Docker build and publish
  2. # 参见: https://docs.docker.com/build/ci/github-actions/multi-platform/
  3. on:
  4. push:
  5. branches: [ "beta" ]
  6. # Publish semver tags as releases.
  7. tags: [ 'v*.*.*' ]
  8. pull_request:
  9. branches: [ "master" ]
  10. env:
  11. # Use docker.io for Docker Hub if empty
  12. REGISTRY: docker.io
  13. # github.repository as <account>/<repo>
  14. IMAGE_NAME: ${{ github.repository }}
  15. jobs:
  16. build:
  17. runs-on: ubuntu-latest
  18. strategy:
  19. fail-fast: false
  20. matrix:
  21. platform:
  22. - linux/i386
  23. - linux/amd64
  24. - linux/arm/v6
  25. - linux/arm/v7
  26. - linux/arm64
  27. permissions:
  28. contents: read
  29. packages: write
  30. # This is used to complete the identity challenge
  31. # with sigstore/fulcio when running outside of PRs.
  32. id-token: write
  33. steps:
  34. - name: Prepare
  35. run: |
  36. platform=${{ matrix.platform }}
  37. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  38. - name: Checkout repository
  39. uses: actions/checkout@v4
  40. # Install the cosign tool except on PR
  41. # https://github.com/sigstore/cosign-installer
  42. - name: Install cosign
  43. if: github.event_name != 'pull_request'
  44. uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1
  45. with:
  46. cosign-release: 'v2.1.1'
  47. - name: Docker meta
  48. id: meta
  49. uses: docker/metadata-action@v5
  50. with:
  51. images: ${{ env.IMAGE_NAME }}
  52. - name: Set up QEMU
  53. uses: docker/setup-qemu-action@v3
  54. # Set up BuildKit Docker container builder to be able to build
  55. # multi-platform images and export cache
  56. # https://github.com/docker/setup-buildx-action
  57. - name: Set up Docker Buildx
  58. uses: docker/setup-buildx-action@v3
  59. # Login against a Docker registry except on PR
  60. # https://github.com/docker/login-action
  61. - name: Log into registry ${{ env.REGISTRY }}
  62. if: github.event_name != 'pull_request'
  63. uses: docker/login-action@v3
  64. with:
  65. registry: ${{ env.REGISTRY }}
  66. username: ${{ github.actor }}
  67. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  68. # Build and push Docker image with Buildx (don't push on PR)
  69. # https://github.com/docker/build-push-action
  70. - name: Build and push Docker image
  71. id: build
  72. uses: docker/build-push-action@v5
  73. with:
  74. context: .
  75. platforms: ${{ matrix.platform }}
  76. push: ${{ github.event_name != 'pull_request' }}
  77. labels: ${{ steps.meta.outputs.labels }}
  78. outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  79. - name: Export digest
  80. run: |
  81. mkdir -p /tmp/digests
  82. digest="${{ steps.build.outputs.digest }}"
  83. touch "/tmp/digests/${digest#sha256:}"
  84. - name: Upload digest
  85. uses: actions/upload-artifact@v4
  86. with:
  87. name: digests-${{ env.PLATFORM_PAIR }}
  88. path: /tmp/digests/*
  89. if-no-files-found: error
  90. retention-days: 1
  91. merge:
  92. runs-on: ubuntu-latest
  93. needs:
  94. - build
  95. steps:
  96. - name: Download digests
  97. uses: actions/download-artifact@v4
  98. with:
  99. path: /tmp/digests
  100. pattern: digests-*
  101. merge-multiple: true
  102. - name: Set up Docker Buildx
  103. uses: docker/setup-buildx-action@v3
  104. - name: Docker meta
  105. id: meta
  106. uses: docker/metadata-action@v5
  107. with:
  108. images: ${{ env.IMAGE_NAME }}
  109. - name: Login to Docker Hub
  110. uses: docker/login-action@v3
  111. with:
  112. username: ${{ github.actor }}
  113. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  114. - name: Create manifest list and push
  115. working-directory: /tmp/digests
  116. run: |
  117. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  118. $(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
  119. - name: Inspect image
  120. run: |
  121. docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}