build_and_push_docker_images.yaml 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. name: Build and push docker images
  2. concurrency:
  3. group: docker-build-${{ github.event_name }}-${{ inputs.profile }}-${{ github.ref }}
  4. cancel-in-progress: true
  5. on:
  6. workflow_call:
  7. inputs:
  8. profile:
  9. required: true
  10. type: string
  11. latest:
  12. required: false
  13. type: string
  14. default: false
  15. publish:
  16. required: false
  17. type: boolean
  18. default: false
  19. secrets:
  20. DOCKER_HUB_USER:
  21. required: false
  22. DOCKER_HUB_TOKEN:
  23. required: false
  24. AWS_ACCESS_KEY_ID:
  25. required: false
  26. AWS_SECRET_ACCESS_KEY:
  27. required: false
  28. AWS_DEFAULT_REGION:
  29. required: false
  30. AWS_S3_BUCKET:
  31. required: false
  32. AWS_CLOUDFRONT_ID:
  33. required: false
  34. workflow_dispatch:
  35. inputs:
  36. ref:
  37. required: false
  38. profile:
  39. required: false
  40. type: string
  41. default: 'emqx'
  42. latest:
  43. required: false
  44. type: boolean
  45. default: false
  46. publish:
  47. required: false
  48. type: boolean
  49. default: false
  50. permissions:
  51. contents: read
  52. jobs:
  53. build:
  54. runs-on: ${{ github.repository_owner == 'emqx' && fromJSON(format('["self-hosted","ephemeral","linux","{0}"]', matrix.arch)) || 'ubuntu-22.04' }}
  55. outputs:
  56. PKG_VSN: ${{ steps.build.outputs.PKG_VSN }}
  57. strategy:
  58. fail-fast: false
  59. matrix:
  60. profile:
  61. - ${{ inputs.profile }}
  62. - ${{ inputs.profile }}-elixir
  63. arch:
  64. - x64
  65. - arm64
  66. steps:
  67. - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
  68. with:
  69. ref: ${{ github.event.inputs.ref }}
  70. - name: build release tarball
  71. id: build
  72. env:
  73. BUILDER_SYSTEM: force_docker
  74. run: |
  75. source ./env.sh
  76. ./scripts/buildx.sh --profile ${{ matrix.profile }} --pkgtype tgz --builder "$EMQX_DOCKER_BUILD_FROM"
  77. PKG_VSN=$(docker run --rm -v $(pwd):$(pwd) -w $(pwd) -u $(id -u) "$EMQX_DOCKER_BUILD_FROM" ./pkg-vsn.sh "${{ matrix.profile }}")
  78. echo "PKG_VSN=$PKG_VSN" >> "$GITHUB_OUTPUT"
  79. - uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
  80. with:
  81. name: "${{ matrix.profile }}-${{ matrix.arch == 'x64' && 'amd64' || 'arm64' }}.tar.gz"
  82. path: "_packages/emqx*/emqx-*.tar.gz"
  83. retention-days: 7
  84. overwrite: true
  85. if-no-files-found: error
  86. docker:
  87. runs-on: ${{ endsWith(github.repository, '/emqx') && 'ubuntu-22.04' || fromJSON('["self-hosted","ephemeral","linux","x64"]') }}
  88. needs:
  89. - build
  90. defaults:
  91. run:
  92. shell: bash
  93. strategy:
  94. fail-fast: false
  95. matrix:
  96. profile:
  97. - ["${{ inputs.profile }}", "${{ inputs.profile == 'emqx' && 'docker.io,public.ecr.aws' || 'docker.io' }}"]
  98. - ["${{ inputs.profile }}-elixir", "${{ inputs.profile == 'emqx' && 'docker.io,public.ecr.aws' || 'docker.io' }}"]
  99. env:
  100. PROFILE: ${{ matrix.profile[0] }}
  101. DOCKER_REGISTRY: ${{ matrix.profile[1] }}
  102. DOCKER_ORG: ${{ github.repository_owner }}
  103. DOCKER_LATEST: ${{ inputs.latest }}
  104. PKG_VSN: ${{ needs.build.outputs.PKG_VSN }}
  105. EMQX_SOURCE_TYPE: tgz
  106. steps:
  107. - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
  108. with:
  109. ref: ${{ github.event.inputs.ref }}
  110. - uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
  111. with:
  112. pattern: "${{ matrix.profile[0] }}-*.tar.gz"
  113. path: _packages
  114. merge-multiple: true
  115. - name: Move artifacts to root directory
  116. env:
  117. PROFILE: ${{ inputs.profile }}
  118. run: |
  119. ls -lR _packages/$PROFILE
  120. mv _packages/$PROFILE/*.tar.gz ./
  121. - name: Enable containerd image store on Docker Engine
  122. run: |
  123. echo "$(sudo cat /etc/docker/daemon.json | jq '. += {"features": {"containerd-snapshotter": true}}')" > daemon.json
  124. sudo mv daemon.json /etc/docker/daemon.json
  125. sudo systemctl restart docker
  126. - uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
  127. - uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1
  128. - name: Login to hub.docker.com
  129. uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
  130. if: inputs.publish && contains(matrix.profile[1], 'docker.io')
  131. with:
  132. username: ${{ secrets.DOCKER_HUB_USER }}
  133. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  134. - name: Login to AWS ECR
  135. uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
  136. if: inputs.publish && contains(matrix.profile[1], 'public.ecr.aws')
  137. with:
  138. registry: public.ecr.aws
  139. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  140. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  141. ecr: true
  142. - name: Build docker image
  143. env:
  144. DOCKER_PUSH: false
  145. DOCKER_BUILD_NOCACHE: true
  146. run: |
  147. ./build ${PROFILE} docker
  148. echo "Built tags:"
  149. echo "==========="
  150. cat .emqx_docker_image_tags
  151. echo "==========="
  152. echo "_EMQX_DOCKER_IMAGE_TAG=$(head -n 1 .emqx_docker_image_tags)" >> $GITHUB_ENV
  153. - name: Verify that size of docker image is less than 300 MB
  154. run: |
  155. docker image inspect $_EMQX_DOCKER_IMAGE_TAG --format='{{.Size}}' | xargs -I {} test {} -lt 300000000
  156. - name: smoke test
  157. timeout-minutes: 1
  158. run: |
  159. for tag in $(cat .emqx_docker_image_tags); do
  160. CID=$(docker run -d -p 18083:18083 $tag)
  161. HTTP_PORT=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "18083/tcp") 0).HostPort}}' $CID)
  162. ./scripts/test/emqx-smoke-test.sh localhost $HTTP_PORT
  163. docker rm -f $CID
  164. done
  165. - name: dashboard tests
  166. working-directory: ./scripts/ui-tests
  167. timeout-minutes: 5
  168. run: |
  169. set -eu
  170. docker compose up --abort-on-container-exit --exit-code-from selenium
  171. docker compose rm -fsv
  172. - name: test node_dump
  173. run: |
  174. CID=$(docker run -d -P $_EMQX_DOCKER_IMAGE_TAG)
  175. docker exec -t -u root -w /root $CID bash -c 'apt-get -y update && apt-get -y install net-tools'
  176. docker exec -t -u root $CID node_dump
  177. docker rm -f $CID
  178. - name: export docker image
  179. run: |
  180. docker save "${_EMQX_DOCKER_IMAGE_TAG}" | gzip > $PROFILE-docker-$PKG_VSN.tar.gz
  181. - uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
  182. with:
  183. name: "${{ env.PROFILE }}-docker"
  184. path: "${{ env.PROFILE }}-docker-${{ env.PKG_VSN }}.tar.gz"
  185. retention-days: 7
  186. - name: Publish docker image
  187. if: inputs.publish || github.repository_owner != 'emqx'
  188. env:
  189. DOCKER_PUSH: true
  190. DOCKER_BUILD_NOCACHE: false
  191. DOCKER_PLATFORMS: linux/amd64,linux/arm64
  192. DOCKER_LOAD: false
  193. run: |
  194. ./build ${PROFILE} docker
  195. - name: Build and publish docker image with Snowflake ODBC driver
  196. if: (inputs.publish || github.repository_owner != 'emqx') && matrix.profile[0] == 'emqx-enterprise'
  197. env:
  198. DOCKER_PUSH: true
  199. DOCKER_BUILD_NOCACHE: false
  200. DOCKER_PLATFORMS: linux/amd64,linux/arm64
  201. DOCKER_LOAD: false
  202. EMQX_DOCKERFILE: deploy/docker/Dockerfile.sfodbc
  203. run: |
  204. export BUILD_FROM="${_EMQX_DOCKER_IMAGE_TAG}"
  205. export EMQX_IMAGE_TAG="${_EMQX_DOCKER_IMAGE_TAG##docker.io/}-sf"
  206. ./build ${PROFILE} docker
  207. - uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
  208. if: inputs.publish || github.repository_owner != 'emqx'
  209. with:
  210. aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
  211. aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  212. aws-region: ${{ secrets.AWS_DEFAULT_REGION }}
  213. - name: upload to aws s3
  214. if: inputs.publish || github.repository_owner != 'emqx'
  215. env:
  216. AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
  217. AWS_CLOUDFRONT_ID: ${{ secrets.AWS_CLOUDFRONT_ID }}
  218. ORIG_PROFILE: ${{ inputs.profile }}
  219. run: |
  220. set -xeuo pipefail
  221. if [ $ORIG_PROFILE = 'emqx' ]; then
  222. s3dir="emqx-ce/v$PKG_VSN"
  223. elif [ $ORIG_PROFILE = 'emqx-enterprise' ]; then
  224. s3dir="emqx-ee/e$PKG_VSN"
  225. else
  226. echo "unknown profile $ORIG_PROFILE"
  227. exit 1
  228. fi
  229. docker pull --platform linux/amd64 "${_EMQX_DOCKER_IMAGE_TAG}"
  230. docker save "${_EMQX_DOCKER_IMAGE_TAG}" | gzip > "$PROFILE-$PKG_VSN-docker-amd64.tar.gz"
  231. docker pull --platform linux/arm64 "${_EMQX_DOCKER_IMAGE_TAG}"
  232. docker save "${_EMQX_DOCKER_IMAGE_TAG}" | gzip > "$PROFILE-$PKG_VSN-docker-arm64.tar.gz"
  233. ls -lh
  234. aws s3 cp "$PROFILE-$PKG_VSN-docker-amd64.tar.gz" "s3://$AWS_S3_BUCKET/$s3dir/"
  235. aws s3 cp "$PROFILE-$PKG_VSN-docker-arm64.tar.gz" "s3://$AWS_S3_BUCKET/$s3dir/"
  236. aws cloudfront create-invalidation --distribution-id "$AWS_CLOUDFRONT_ID" --paths "/$s3dir/*docker*"