build_and_push_docker_images.yaml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. name: Build and push docker images
  2. concurrency:
  3. group: docker-build-${{ github.event_name }}-${{ github.ref }}
  4. cancel-in-progress: true
  5. on:
  6. push:
  7. tags:
  8. - v*
  9. - e*
  10. - docker-latest-*
  11. workflow_dispatch:
  12. inputs:
  13. branch_or_tag:
  14. required: false
  15. profile:
  16. required: false
  17. default: 'emqx'
  18. is_latest:
  19. required: false
  20. default: false
  21. jobs:
  22. prepare:
  23. runs-on: ubuntu-22.04
  24. # prepare source with any OTP version, no need for a matrix
  25. container: "ghcr.io/emqx/emqx-builder/5.0-33:1.13.4-24.3.4.2-3-ubuntu22.04"
  26. outputs:
  27. PROFILE: ${{ steps.get_profile.outputs.PROFILE }}
  28. EDITION: ${{ steps.get_profile.outputs.EDITION }}
  29. IS_LATEST: ${{ steps.get_profile.outputs.IS_LATEST }}
  30. IS_EXACT_TAG: ${{ steps.get_profile.outputs.IS_EXACT_TAG }}
  31. VERSION: ${{ steps.get_profile.outputs.VERSION }}
  32. steps:
  33. - uses: actions/checkout@v3
  34. with:
  35. ref: ${{ github.event.inputs.branch_or_tag }} # when input is not given, the event tag is used
  36. path: source
  37. fetch-depth: 0
  38. - name: Get profiles to build
  39. id: get_profile
  40. env:
  41. INPUTS_PROFILE: ${{ github.event.inputs.profile }}
  42. run: |
  43. cd source
  44. # tag docker-latest-ce or docker-latest-ee
  45. if git describe --tags --exact --match 'docker-latest-*' 2>/dev/null; then
  46. echo 'is_latest=true due to docker-latest-* tag'
  47. is_latest=true
  48. elif [ "${{ inputs.is_latest }}" = "true" ]; then
  49. echo 'is_latest=true due to manual input from workflow_dispatch'
  50. is_latest=true
  51. else
  52. echo 'is_latest=false'
  53. is_latest=false
  54. fi
  55. # resolve profile
  56. if git describe --tags --match "v*" --exact; then
  57. echo "This is an exact git tag, will publish images"
  58. is_exact='true'
  59. PROFILE=emqx
  60. elif git describe --tags --match "e*" --exact; then
  61. echo "This is an exact git tag, will publish images"
  62. is_exact='true'
  63. PROFILE=emqx-enterprise
  64. else
  65. echo "This is NOT an exact git tag, will not publish images"
  66. is_exact='false'
  67. fi
  68. case "${PROFILE:-$INPUTS_PROFILE}" in
  69. emqx)
  70. EDITION='Opensource'
  71. ;;
  72. emqx-enterprise)
  73. EDITION='Enterprise'
  74. ;;
  75. *)
  76. echo "ERROR: Failed to resolve build profile"
  77. exit 1
  78. ;;
  79. esac
  80. VSN="$(./pkg-vsn.sh "$PROFILE")"
  81. echo "Building emqx/$PROFILE:$VSN image (latest=$is_latest)"
  82. echo "Push = $is_exact"
  83. echo "IS_LATEST=$is_latest" >> $GITHUB_OUTPUT
  84. echo "IS_EXACT_TAG=$is_exact" >> $GITHUB_OUTPUT
  85. echo "PROFILE=$PROFILE" >> $GITHUB_OUTPUT
  86. echo "EDITION=$EDITION" >> $GITHUB_OUTPUT
  87. echo "VERSION=$VSN" >> $GITHUB_OUTPUT
  88. - name: get_all_deps
  89. env:
  90. PROFILE: ${{ steps.get_profile.outputs.PROFILE }}
  91. run: |
  92. PROFILE=$PROFILE make -C source deps-$PROFILE
  93. zip -ryq source.zip source/* source/.[^.]*
  94. - uses: actions/upload-artifact@v3
  95. with:
  96. name: source
  97. path: source.zip
  98. docker:
  99. runs-on: ubuntu-22.04
  100. needs: prepare
  101. strategy:
  102. fail-fast: false
  103. matrix:
  104. profile:
  105. - "${{ needs.prepare.outputs.PROFILE }}"
  106. registry:
  107. - 'docker.io'
  108. - 'public.ecr.aws'
  109. os:
  110. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  111. # NOTE: 'otp' and 'elixir' are to configure emqx-builder image
  112. # only support latest otp and elixir, not a matrix
  113. builder:
  114. - 5.0-33 # update to latest
  115. otp:
  116. - 24.3.4.2-3 # switch to 25 once ready to release 5.1
  117. elixir:
  118. - 'no_elixir'
  119. - '1.13.4' # update to latest
  120. exclude: # TODO: publish enterprise to ecr too?
  121. - registry: 'public.ecr.aws'
  122. profile: emqx-enterprise
  123. steps:
  124. - uses: actions/download-artifact@v3
  125. with:
  126. name: source
  127. path: .
  128. - name: unzip source code
  129. run: unzip -q source.zip
  130. - uses: docker/setup-qemu-action@v2
  131. - uses: docker/setup-buildx-action@v2
  132. - name: Login to hub.docker.com
  133. uses: docker/login-action@v2
  134. if: matrix.registry == 'docker.io'
  135. with:
  136. username: ${{ secrets.DOCKER_HUB_USER }}
  137. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  138. - name: Login to AWS ECR
  139. uses: docker/login-action@v2
  140. if: matrix.registry == 'public.ecr.aws'
  141. with:
  142. registry: public.ecr.aws
  143. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  144. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  145. ecr: true
  146. - name: prepare for docker/metadata-action
  147. id: pre-meta
  148. shell: bash
  149. run: |
  150. extra_labels=
  151. img_suffix=
  152. if [ "${{ matrix.elixir }}" != 'no_elixir' ]; then
  153. img_suffix="-elixir"
  154. extra_labels="org.opencontainers.image.elixir.version=${{ matrix.elixir }}"
  155. fi
  156. echo "img_suffix=$img_suffix" >> $GITHUB_OUTPUT
  157. echo "extra_labels=$extra_labels" >> $GITHUB_OUTPUT
  158. - uses: docker/metadata-action@v4
  159. id: meta
  160. with:
  161. images: |
  162. ${{ matrix.registry }}/${{ github.repository_owner }}/${{ matrix.profile }}
  163. flavor: |
  164. suffix=${{ steps.pre-meta.outputs.img_suffix }}
  165. tags: |
  166. type=raw,value=${{ needs.prepare.outputs.VERSION }}
  167. type=raw,value=latest,enable=${{ needs.prepare.outputs.IS_LATEST }}
  168. labels: |
  169. org.opencontainers.image.otp.version=${{ matrix.otp }}
  170. org.opencontainers.image.edition=${{ needs.prepare.outputs.EDITION }}
  171. ${{ steps.pre-meta.outputs.extra_labels }}
  172. - uses: docker/build-push-action@v3
  173. with:
  174. push: ${{ needs.prepare.outputs.IS_EXACT_TAG == 'true' || github.repository_owner != 'emqx' }}
  175. pull: true
  176. no-cache: true
  177. platforms: linux/amd64,linux/arm64
  178. tags: ${{ steps.meta.outputs.tags }}
  179. labels: ${{ steps.meta.outputs.labels }}
  180. build-args: |
  181. EMQX_NAME=${{ matrix.profile }}${{ steps.pre-meta.outputs.img_suffix }}
  182. file: source/${{ matrix.os[2] }}
  183. context: source