release.yml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. name: release
  2. on:
  3. push:
  4. branches:
  5. - "**"
  6. tags:
  7. - "v*.*.*"
  8. pull_request:
  9. env:
  10. DOCKERHUB_REPO: ${{ secrets.DOCKERHUB_USERNAME != '' && format('{0}/{1}', secrets.DOCKERHUB_USERNAME, 'aiproxy') || '' }}
  11. GHCR_REPO: ghcr.io/${{ github.repository }}
  12. jobs:
  13. release:
  14. name: Release
  15. runs-on: ubuntu-24.04
  16. strategy:
  17. fail-fast: false
  18. matrix:
  19. targets:
  20. - GOOS: linux
  21. GOARCH: arm64
  22. - GOOS: linux
  23. GOARCH: amd64
  24. - GOOS: darwin
  25. GOARCH: arm64
  26. - GOOS: darwin
  27. GOARCH: amd64
  28. - GOOS: windows
  29. GOARCH: arm64
  30. EXT: .exe
  31. - GOOS: windows
  32. GOARCH: amd64
  33. EXT: .exe
  34. - GOOS: windows
  35. GOARCH: arm64
  36. EXT: .exe
  37. steps:
  38. - name: Checkout
  39. uses: actions/checkout@v4
  40. - name: Setup Go
  41. uses: actions/setup-go@v5
  42. with:
  43. go-version: "1.23"
  44. - name: Build
  45. run: |
  46. export GOOS=${{ matrix.targets.GOOS }}
  47. export GOARCH=${{ matrix.targets.GOARCH }}
  48. go build -trimpath -tags "jsoniter" -ldflags "-s -w" -o aiproxy${{ matrix.targets.EXT }}
  49. - name: Get release meta
  50. if: ${{ startsWith(github.ref, 'refs/tags/') }}
  51. id: release_meta
  52. run: |
  53. version=${GITHUB_REF/refs\/tags\/v/}
  54. echo "version: ${version}"
  55. prerelease=$(echo ${version} | grep -E 'rc|beta|alpha' || true)
  56. release_name="Version ${version}"
  57. echo "release_name: ${release_name}"
  58. if [ -n "${prerelease}" ]; then
  59. prerelease=true
  60. release_name="${release_name} (Prerelease)"
  61. fi
  62. tag_name="v${version}"
  63. echo "prerelease: ${prerelease}"
  64. echo "tag_name: ${tag_name}"
  65. echo ::set-output name=PRERELEASE::${prerelease}
  66. echo ::set-output name=RELEASE_NAME::${release_name}
  67. echo ::set-output name=TAG_NAME::${tag_name}
  68. - name: Release
  69. uses: softprops/action-gh-release@v2
  70. if: ${{ startsWith(github.ref, 'refs/tags/') }}
  71. with:
  72. token: ${{ secrets.GITHUB_TOKEN }}
  73. draft: false
  74. prerelease: ${{ steps.release_meta.outputs.PRERELEASE }}
  75. append_body: false
  76. fail_on_unmatched_files: true
  77. name: ${{ steps.release_meta.outputs.RELEASE_NAME }}
  78. tag_name: ${{ steps.release_meta.outputs.TAG_NAME }}
  79. files: |
  80. aiproxy${{ matrix.targets.EXT }}
  81. build-docker:
  82. name: Release Docker
  83. strategy:
  84. matrix:
  85. include:
  86. - arch: amd64
  87. - arch: arm64
  88. runs-on: ubuntu-24.04-arm
  89. runs-on: ${{ matrix.runs-on || 'ubuntu-24.04' }}
  90. steps:
  91. - name: Login to GitHub Container Registry
  92. if: ${{ github.event_name != 'pull_request' }}
  93. uses: docker/login-action@v3
  94. with:
  95. registry: ghcr.io
  96. username: ${{ github.actor }}
  97. password: ${{ secrets.GITHUB_TOKEN }}
  98. - name: Login to DockerHub
  99. uses: docker/login-action@v3
  100. if: ${{ github.event_name != 'pull_request' && env.DOCKERHUB_REPO }}
  101. with:
  102. username: ${{ secrets.DOCKERHUB_USERNAME }}
  103. password: ${{ secrets.DOCKERHUB_TOKEN }}
  104. - name: Checkout
  105. uses: actions/checkout@v4
  106. - name: Set up Docker Buildx
  107. uses: docker/setup-buildx-action@v3
  108. - name: Extract metadata (tags, labels) for Docker
  109. id: meta
  110. uses: docker/metadata-action@v5
  111. with:
  112. images: |
  113. ${{ env.DOCKERHUB_REPO }}
  114. ${{ env.GHCR_REPO }}
  115. - name: Build for ${{ matrix.arch }}
  116. id: build
  117. uses: docker/build-push-action@v6
  118. with:
  119. context: .
  120. labels: ${{ steps.meta.outputs.labels }}
  121. platforms: linux/${{ matrix.arch }}
  122. outputs: type=image,"name=${{ env.DOCKERHUB_REPO && format('{0},{1}', env.DOCKERHUB_REPO, env.GHCR_REPO) || env.GHCR_REPO }}",name-canonical=true,push-by-digest=${{ github.event_name != 'pull_request' }},push=${{ github.event_name != 'pull_request' }}
  123. - name: Export digest
  124. run: |
  125. mkdir -p ${{ runner.temp }}/digests
  126. digest="${{ steps.build.outputs.digest }}"
  127. touch "${{ runner.temp }}/digests/${digest#sha256:}"
  128. - name: Upload digest
  129. uses: actions/upload-artifact@v4
  130. with:
  131. name: digests-${{ matrix.arch }}
  132. path: ${{ runner.temp }}/digests/*
  133. if-no-files-found: error
  134. retention-days: 1
  135. release-docker:
  136. name: Push Docker Images
  137. needs: build-docker
  138. runs-on: ubuntu-24.04
  139. if: ${{ github.event_name != 'pull_request' }}
  140. steps:
  141. - name: Login to GitHub Container Registry
  142. uses: docker/login-action@v3
  143. with:
  144. registry: ghcr.io
  145. username: ${{ github.actor }}
  146. password: ${{ secrets.GITHUB_TOKEN }}
  147. - name: Login to DockerHub
  148. uses: docker/login-action@v3
  149. if: ${{ github.event_name != 'pull_request' && env.DOCKERHUB_REPO }}
  150. with:
  151. username: ${{ secrets.DOCKERHUB_USERNAME }}
  152. password: ${{ secrets.DOCKERHUB_TOKEN }}
  153. - name: Download digests
  154. uses: actions/download-artifact@v4
  155. with:
  156. path: ${{ runner.temp }}/digests
  157. pattern: digests-*
  158. merge-multiple: true
  159. - name: Set up Docker Buildx
  160. uses: docker/setup-buildx-action@v3
  161. - name: Extract metadata (tags, labels) for Docker
  162. id: meta
  163. uses: docker/metadata-action@v5
  164. with:
  165. images: |
  166. ${{ env.DOCKERHUB_REPO }}
  167. ${{ env.GHCR_REPO }}
  168. tags: |
  169. type=ref,event=branch
  170. type=ref,event=pr
  171. type=ref,event=tag
  172. type=semver,pattern={{version}}
  173. type=semver,pattern={{major}}.{{minor}}
  174. type=semver,pattern={{major}}
  175. type=sha
  176. - name: Create manifest list and push
  177. working-directory: ${{ runner.temp }}/digests
  178. run: |
  179. if [ -n "${{ env.DOCKERHUB_REPO }}" ]; then
  180. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  181. $(printf '${{ env.DOCKERHUB_REPO }}@sha256:%s ' *)
  182. fi
  183. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  184. $(printf '${{ env.GHCR_REPO }}@sha256:%s ' *)
  185. - name: Inspect image
  186. run: |
  187. if [ -n "${{ env.DOCKERHUB_REPO }}" ]; then
  188. docker buildx imagetools inspect ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.version }}
  189. fi
  190. docker buildx imagetools inspect ${{ env.GHCR_REPO }}:${{ steps.meta.outputs.version }}