build_and_push_docker_images.yaml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. release:
  11. types:
  12. - published
  13. workflow_dispatch:
  14. inputs:
  15. branch_or_tag:
  16. required: false
  17. profile:
  18. required: false
  19. jobs:
  20. prepare:
  21. runs-on: ubuntu-20.04
  22. # prepare source with any OTP version, no need for a matrix
  23. container: "ghcr.io/emqx/emqx-builder/5.0-26:1.13.4-24.3.4.2-1-ubuntu20.04"
  24. outputs:
  25. BUILD_PROFILE: ${{ steps.get_profile.outputs.BUILD_PROFILE }}
  26. IS_DOCKER_LATEST: ${{ steps.get_profile.outputs.IS_DOCKER_LATEST }}
  27. IS_EXACT_TAG: ${{ steps.get_profile.outputs.IS_EXACT_TAG }}
  28. DOCKER_TAG_VERSION: ${{ steps.get_profile.outputs.DOCKER_TAG_VERSION }}
  29. steps:
  30. - uses: actions/checkout@v3
  31. with:
  32. ref: ${{ github.event.inputs.branch_or_tag }} # when input is not given, the event tag is used
  33. path: source
  34. fetch-depth: 0
  35. - name: Get profiles to build
  36. id: get_profile
  37. run: |
  38. cd source
  39. tag=${{ github.ref }}
  40. # tag docker-latest-ce or docker-latest-ee
  41. if git describe --tags --exact --match 'docker-latest-*' 2>/dev/null; then
  42. echo 'docker_latest=true due to docker-latest-* tag'
  43. docker_latest=true
  44. elif [ "${{ github.event_name }}" = "release" ]; then
  45. echo 'docker_latest=true due to release'
  46. docker_latest=true
  47. else
  48. echo 'docker_latest=false'
  49. docker_latest=false
  50. fi
  51. if git describe --tags --match "[v|e]*" --exact; then
  52. echo "This is an exact git tag, will publish images"
  53. is_exact='true'
  54. else
  55. echo "This is NOT an exact git tag, will not publish images"
  56. is_exact='false'
  57. fi
  58. case $tag in
  59. refs/tags/v*)
  60. PROFILE='emqx'
  61. ;;
  62. refs/tags/e*)
  63. PROFILE=emqx-enterprise
  64. ;;
  65. *)
  66. PROFILE=${{ github.event.inputs.profile }}
  67. case "$PROFILE" in
  68. emqx)
  69. true
  70. ;;
  71. emqx-enterprise)
  72. true
  73. ;;
  74. *)
  75. echo "ERROR: Failed to resolve build profile"
  76. exit 1
  77. ;;
  78. esac
  79. ;;
  80. esac
  81. VSN="$(./pkg-vsn.sh "$PROFILE")"
  82. echo "Building $PROFILE image with tag $VSN (latest=$docker_latest)"
  83. echo "IS_DOCKER_LATEST=$docker_latest" >> $GITHUB_OUTPUT
  84. echo "IS_EXACT_TAG=$is_exact" >> $GITHUB_OUTPUT
  85. echo "BUILD_PROFILE=$PROFILE" >> $GITHUB_OUTPUT
  86. echo "DOCKER_TAG_VERSION=$VSN" >> $GITHUB_OUTPUT
  87. - name: get_all_deps
  88. run: |
  89. make -C source deps-all
  90. zip -ryq source.zip source/* source/.[^.]*
  91. - uses: actions/upload-artifact@v3
  92. with:
  93. name: source
  94. path: source.zip
  95. docker:
  96. runs-on: ${{ matrix.arch[1] }}
  97. needs: prepare
  98. strategy:
  99. fail-fast: false
  100. matrix:
  101. arch:
  102. - [amd64, ubuntu-20.04]
  103. - [arm64, aws-arm64]
  104. profile:
  105. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  106. registry:
  107. - 'docker.io'
  108. - 'public.ecr.aws'
  109. os:
  110. - [alpine3.15.1, "alpine:3.15.1", "deploy/docker/Dockerfile.alpine"]
  111. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  112. # NOTE: 'otp' and 'elixir' are to configure emqx-builder image
  113. # only support latest otp and elixir, not a matrix
  114. builder:
  115. - 5.0-26 # update to latest
  116. otp:
  117. - 24.3.4.2-1 # switch to 25 once ready to release 5.1
  118. 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: AutoModality/action-clean@v1
  125. if: matrix.arch[1] == 'aws-arm64'
  126. - uses: actions/download-artifact@v3
  127. with:
  128. name: source
  129. path: .
  130. - name: unzip source code
  131. run: unzip -q source.zip
  132. - uses: docker/setup-buildx-action@v2
  133. - name: Login for docker.
  134. uses: docker/login-action@v2
  135. if: matrix.registry == 'docker.io'
  136. with:
  137. username: ${{ secrets.DOCKER_HUB_USER }}
  138. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  139. - name: Login for AWS ECR
  140. uses: docker/login-action@v2
  141. if: matrix.registry == 'public.ecr.aws'
  142. with:
  143. registry: public.ecr.aws
  144. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  145. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  146. ecr: true
  147. - uses: ./source/.github/actions/docker-meta
  148. id: meta
  149. with:
  150. profile: ${{ matrix.profile }}
  151. registry: ${{ matrix.registry }}
  152. arch: ${{ matrix.arch[0] }}
  153. otp: ${{ matrix.otp }}
  154. builder_base: ${{ matrix.os[0] }}
  155. owner: ${{ github.repository_owner }}
  156. docker_tags: ${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  157. - uses: docker/build-push-action@v3
  158. with:
  159. push: ${{ needs.prepare.outputs.IS_EXACT_TAG == 'true' || github.repository_owner != 'emqx' }}
  160. pull: true
  161. no-cache: true
  162. platforms: linux/${{ matrix.arch[0] }}
  163. tags: ${{ steps.meta.outputs.tags }}
  164. labels: ${{ steps.meta.outputs.labels }}
  165. build-args: |
  166. BUILD_FROM=ghcr.io/emqx/emqx-builder/${{ matrix.builder }}:${{ matrix.elixir }}-${{ matrix.otp }}-${{ matrix.os[0] }}
  167. RUN_FROM=${{ matrix.os[1] }}
  168. EMQX_NAME=${{ steps.meta.outputs.emqx_name }}
  169. file: source/${{ matrix.os[2] }}
  170. context: source
  171. - name: Docker Hub Description
  172. if: matrix.registry == 'docker.io'
  173. uses: peter-evans/dockerhub-description@v3
  174. with:
  175. username: ${{ secrets.DOCKERHUB_USERNAME }}
  176. password: ${{ secrets.DOCKERHUB_PASSWORD }}
  177. repository: "emqx/${{ needs.prepare.outputs.BUILD_PROFILE }}"
  178. readme-filepath: ./source/deploy/docker/README.md
  179. short-description: "The most scalable open-source MQTT broker for IoT, IIoT, connected vehicles, and more."
  180. docker-elixir:
  181. runs-on: ${{ matrix.arch[1] }}
  182. needs: prepare
  183. # do not build elixir images for ee for now
  184. if: needs.prepare.outputs.BUILD_PROFILE == 'emqx'
  185. strategy:
  186. fail-fast: false
  187. matrix:
  188. arch:
  189. - [amd64, ubuntu-20.04]
  190. - [arm64, aws-arm64]
  191. profile:
  192. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  193. registry:
  194. - 'docker.io'
  195. os:
  196. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  197. builder:
  198. - 5.0-26 # update to latest
  199. otp:
  200. - 25.1.2-2 # update to latest
  201. elixir:
  202. - 1.13.4 # update to latest
  203. steps:
  204. - uses: AutoModality/action-clean@v1
  205. if: matrix.arch[1] == 'aws-arm64'
  206. - uses: actions/download-artifact@v3
  207. with:
  208. name: source
  209. path: .
  210. - name: unzip source code
  211. run: unzip -q source.zip
  212. - uses: docker/setup-buildx-action@v2
  213. - name: Login for docker.
  214. uses: docker/login-action@v2
  215. with:
  216. username: ${{ secrets.DOCKER_HUB_USER }}
  217. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  218. - uses: ./source/.github/actions/docker-meta
  219. id: meta
  220. with:
  221. profile: ${{ matrix.profile }}
  222. registry: ${{ matrix.registry }}
  223. arch: ${{ matrix.arch[0] }}
  224. otp: ${{ matrix.otp }}
  225. elixir: ${{ matrix.elixir }}
  226. builder_base: ${{ matrix.os[0] }}
  227. owner: ${{ github.repository_owner }}
  228. docker_tags: ${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  229. - uses: docker/build-push-action@v3
  230. with:
  231. push: ${{ needs.prepare.outputs.IS_EXACT_TAG == 'true' || github.repository_owner != 'emqx' }}
  232. pull: true
  233. no-cache: true
  234. platforms: linux/${{ matrix.arch[0] }}
  235. tags: ${{ steps.meta.outputs.tags }}
  236. labels: ${{ steps.meta.outputs.labels }}
  237. build-args: |
  238. BUILD_FROM=ghcr.io/emqx/emqx-builder/${{ matrix.builder }}:${{ matrix.elixir }}-${{ matrix.otp }}-${{ matrix.os[0] }}
  239. RUN_FROM=${{ matrix.os[1] }}
  240. EMQX_NAME=${{ steps.meta.outputs.emqx_name }}
  241. file: source/${{ matrix.os[2] }}
  242. context: source
  243. docker-push-multi-arch-manifest:
  244. # note, we only run on amd64
  245. if: needs.prepare.outputs.IS_EXACT_TAG
  246. needs:
  247. - prepare
  248. - docker
  249. runs-on: ${{ matrix.arch[1] }}
  250. strategy:
  251. fail-fast: false
  252. matrix:
  253. arch:
  254. - [amd64, ubuntu-20.04]
  255. profile:
  256. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  257. os:
  258. - [alpine3.15.1, "alpine:3.15.1", "deploy/docker/Dockerfile.alpine"]
  259. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  260. # NOTE: only support latest otp version, not a matrix
  261. otp:
  262. - 24.3.4.2-1 # switch to 25 once ready to release 5.1
  263. registry:
  264. - 'docker.io'
  265. - 'public.ecr.aws'
  266. exclude:
  267. - registry: 'public.ecr.aws'
  268. profile: emqx-enterprise
  269. steps:
  270. - uses: actions/download-artifact@v3
  271. with:
  272. name: source
  273. path: .
  274. - name: unzip source code
  275. run: unzip -q source.zip
  276. - uses: docker/login-action@v2
  277. if: matrix.registry == 'docker.io'
  278. with:
  279. username: ${{ secrets.DOCKER_HUB_USER }}
  280. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  281. - uses: docker/login-action@v2
  282. if: matrix.registry == 'public.ecr.aws'
  283. with:
  284. registry: public.ecr.aws
  285. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  286. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  287. ecr: true
  288. - uses: ./source/.github/actions/docker-meta
  289. id: meta
  290. with:
  291. profile: ${{ matrix.profile }}
  292. registry: ${{ matrix.registry }}
  293. arch: ${{ matrix.arch[0] }}
  294. otp: ${{ matrix.otp }}
  295. builder_base: ${{ matrix.os[0] }}
  296. owner: ${{ github.repository_owner }}
  297. docker_tags: ${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  298. - name: update manifest for multiarch image
  299. working-directory: source
  300. run: |
  301. is_latest="${{ needs.prepare.outputs.IS_DOCKER_LATEST }}"
  302. scripts/docker-create-push-manifests.sh "${{ steps.meta.outputs.tags }}" "$is_latest"
  303. docker-elixir-push-multi-arch-manifest:
  304. # note, we only run on amd64
  305. # do not build enterprise elixir images for now
  306. if: needs.prepare.outputs.IS_EXACT_TAG == 'true' && needs.prepare.outputs.BUILD_PROFILE == 'emqx'
  307. needs:
  308. - prepare
  309. - docker-elixir
  310. runs-on: ${{ matrix.arch[1] }}
  311. strategy:
  312. fail-fast: false
  313. matrix:
  314. arch:
  315. - [amd64, ubuntu-20.04]
  316. profile:
  317. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  318. # NOTE: for docker, only support latest otp version, not a matrix
  319. otp:
  320. - 25.1.2-2 # update to latest
  321. elixir:
  322. - 1.13.4 # update to latest
  323. registry:
  324. - 'docker.io'
  325. steps:
  326. - uses: actions/download-artifact@v3
  327. with:
  328. name: source
  329. path: .
  330. - name: unzip source code
  331. run: unzip -q source.zip
  332. - uses: docker/login-action@v2
  333. with:
  334. username: ${{ secrets.DOCKER_HUB_USER }}
  335. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  336. - uses: ./source/.github/actions/docker-meta
  337. id: meta
  338. with:
  339. profile: ${{ matrix.profile }}
  340. registry: ${{ matrix.registry }}
  341. arch: ${{ matrix.arch[0] }}
  342. otp: ${{ matrix.otp }}
  343. elixir: ${{ matrix.elixir }}
  344. builder_base: ${{ matrix.os[0] }}
  345. owner: ${{ github.repository_owner }}
  346. docker_tags: ${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  347. - name: update manifest for multiarch image
  348. working-directory: source
  349. run: |
  350. scripts/docker-create-push-manifests.sh "${{ steps.meta.outputs.tags }}" false