docker-build.yml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. name: Publish Docker image (Multi-arch)
  2. on:
  3. push:
  4. branches:
  5. - main
  6. tags:
  7. - '*'
  8. - '!nightly*'
  9. workflow_dispatch:
  10. inputs:
  11. tag:
  12. description: 'Tag name to build (e.g., v0.10.8)'
  13. required: false
  14. type: string
  15. jobs:
  16. build_single_arch:
  17. name: Build & push (${{ matrix.arch }})
  18. strategy:
  19. fail-fast: false
  20. matrix:
  21. include:
  22. - arch: amd64
  23. platform: linux/amd64
  24. runner: ubuntu-latest
  25. - arch: arm64
  26. platform: linux/arm64
  27. runner: ubuntu-24.04-arm
  28. runs-on: ${{ matrix.runner }}
  29. outputs:
  30. tag: ${{ steps.version.outputs.tag }}
  31. is_tag: ${{ steps.version.outputs.is_tag }}
  32. permissions:
  33. packages: write
  34. contents: read
  35. id-token: write
  36. steps:
  37. - name: Check out
  38. uses: actions/checkout@v4
  39. with:
  40. fetch-depth: ${{ github.event_name == 'workflow_dispatch' && 0 || 1 }}
  41. ref: ${{ github.event.inputs.tag || github.ref }}
  42. - name: Resolve version
  43. id: version
  44. run: |
  45. IS_TAG=false
  46. if [ -n "${{ github.event.inputs.tag }}" ]; then
  47. TAG="${{ github.event.inputs.tag }}"
  48. if ! git rev-parse "refs/tags/$TAG" >/dev/null 2>&1; then
  49. echo "::error::Tag '$TAG' does not exist"
  50. exit 1
  51. fi
  52. IS_TAG=true
  53. elif [[ "${GITHUB_REF}" == refs/tags/* ]]; then
  54. TAG="${GITHUB_REF#refs/tags/}"
  55. IS_TAG=true
  56. else
  57. TAG="$(date +'%Y%m%d')-$(git rev-parse --short HEAD)"
  58. fi
  59. echo "TAG=${TAG}" >> $GITHUB_ENV
  60. echo "tag=${TAG}" >> $GITHUB_OUTPUT
  61. echo "is_tag=${IS_TAG}" >> $GITHUB_OUTPUT
  62. echo "${TAG}" > VERSION
  63. echo "Building version: ${TAG} for ${{ matrix.arch }}"
  64. - name: Compute image tags
  65. id: tags
  66. run: |
  67. TAGS="calciumion/new-api:${TAG}-${{ matrix.arch }}"
  68. if [ "${{ steps.version.outputs.is_tag }}" = "true" ]; then
  69. TAGS="${TAGS}"$'\n'"calciumion/new-api:latest-${{ matrix.arch }}"
  70. else
  71. TAGS="${TAGS}"$'\n'"calciumion/new-api:main-${{ matrix.arch }}"
  72. fi
  73. {
  74. echo "tags<<EOF"
  75. echo "${TAGS}"
  76. echo "EOF"
  77. } >> $GITHUB_OUTPUT
  78. - name: Set up Docker Buildx
  79. uses: docker/setup-buildx-action@v3
  80. - name: Log in to Docker Hub
  81. uses: docker/login-action@v3
  82. with:
  83. username: ${{ secrets.DOCKERHUB_USERNAME }}
  84. password: ${{ secrets.DOCKERHUB_TOKEN }}
  85. - name: Extract metadata (labels)
  86. id: meta
  87. uses: docker/metadata-action@v5
  88. with:
  89. images: calciumion/new-api
  90. - name: Build & push
  91. id: build
  92. uses: docker/build-push-action@v6
  93. with:
  94. context: .
  95. platforms: ${{ matrix.platform }}
  96. push: true
  97. tags: ${{ steps.tags.outputs.tags }}
  98. labels: ${{ steps.meta.outputs.labels }}
  99. cache-from: type=gha
  100. cache-to: type=gha,mode=max
  101. provenance: mode=max
  102. sbom: true
  103. - name: Install cosign
  104. if: steps.version.outputs.is_tag == 'true'
  105. uses: sigstore/cosign-installer@v3
  106. - name: Sign image with cosign
  107. if: steps.version.outputs.is_tag == 'true'
  108. run: cosign sign --yes calciumion/new-api@${{ steps.build.outputs.digest }}
  109. - name: Image summary
  110. run: |
  111. echo "### Docker Image Digest (${{ matrix.arch }})" >> $GITHUB_STEP_SUMMARY
  112. echo '```' >> $GITHUB_STEP_SUMMARY
  113. echo "calciumion/new-api:${TAG}-${{ matrix.arch }}" >> $GITHUB_STEP_SUMMARY
  114. echo "${{ steps.build.outputs.digest }}" >> $GITHUB_STEP_SUMMARY
  115. echo '```' >> $GITHUB_STEP_SUMMARY
  116. create_manifests:
  117. name: Create multi-arch manifests
  118. needs: [build_single_arch]
  119. runs-on: ubuntu-latest
  120. steps:
  121. - name: Set version
  122. run: echo "TAG=${{ needs.build_single_arch.outputs.tag }}" >> $GITHUB_ENV
  123. - name: Log in to Docker Hub
  124. uses: docker/login-action@v3
  125. with:
  126. username: ${{ secrets.DOCKERHUB_USERNAME }}
  127. password: ${{ secrets.DOCKERHUB_TOKEN }}
  128. - name: Create & push manifest (version)
  129. run: |
  130. docker buildx imagetools create \
  131. -t calciumion/new-api:${TAG} \
  132. calciumion/new-api:${TAG}-amd64 \
  133. calciumion/new-api:${TAG}-arm64
  134. - name: Create & push manifest (latest)
  135. if: needs.build_single_arch.outputs.is_tag == 'true'
  136. run: |
  137. docker buildx imagetools create \
  138. -t calciumion/new-api:latest \
  139. calciumion/new-api:latest-amd64 \
  140. calciumion/new-api:latest-arm64
  141. - name: Create & push manifest (main)
  142. if: needs.build_single_arch.outputs.is_tag != 'true'
  143. run: |
  144. docker buildx imagetools create \
  145. -t calciumion/new-api:main \
  146. calciumion/new-api:main-amd64 \
  147. calciumion/new-api:main-arm64
  148. - name: Manifest summary
  149. run: |
  150. echo "### Multi-arch Manifest" >> $GITHUB_STEP_SUMMARY
  151. echo '```' >> $GITHUB_STEP_SUMMARY
  152. docker buildx imagetools inspect calciumion/new-api:${TAG} >> $GITHUB_STEP_SUMMARY
  153. echo '```' >> $GITHUB_STEP_SUMMARY