build_and_push_docker_images.yaml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. workflow_dispatch:
  11. inputs:
  12. branch_or_tag:
  13. required: false
  14. profile:
  15. required: false
  16. jobs:
  17. prepare:
  18. runs-on: ubuntu-20.04
  19. # prepare source with any OTP version, no need for a matrix
  20. container: "ghcr.io/emqx/emqx-builder/5.0-17:1.13.4-24.2.1-1-ubuntu20.04"
  21. outputs:
  22. BUILD_PROFILE: ${{ steps.get_profile.outputs.BUILD_PROFILE }}
  23. IS_DOCKER_LATEST: ${{ steps.get_profile.outputs.IS_DOCKER_LATEST }}
  24. IS_EXACT_TAG: ${{ steps.get_profile.outputs.IS_EXACT_TAG }}
  25. DOCKER_TAG_VERSION: ${{ steps.get_profile.outputs.DOCKER_TAG_VERSION }}
  26. steps:
  27. - uses: actions/checkout@v2
  28. with:
  29. ref: ${{ github.event.inputs.branch_or_tag }} # when input is not given, the event tag is used
  30. path: source
  31. fetch-depth: 0
  32. - name: Get profiles to build
  33. id: get_profile
  34. run: |
  35. cd source
  36. tag=${{ github.ref }}
  37. # tag docker-latest-ce or docker-latest-ee
  38. if git describe --tags --exact --match 'docker-latest-*'; then
  39. docker_latest=true
  40. else
  41. docker_latest=false
  42. fi
  43. echo "::set-output name=IS_DOCKER_LATEST::${docker_latest}"
  44. if git describe --tags --match "[v|e]*" --exact; then
  45. echo "This is an exact git tag, will publish images"
  46. is_exact='true'
  47. else
  48. echo "This is NOT an exact git tag, will not publish images"
  49. is_exact='false'
  50. fi
  51. echo "::set-output name=IS_EXACT_TAG::${is_exact}"
  52. case $tag in
  53. refs/tags/v*)
  54. PROFILE='emqx'
  55. ;;
  56. refs/tags/e*)
  57. PROFILE=emqx-enterprise
  58. ;;
  59. *)
  60. PROFILE=${{ github.event.inputs.profile }}
  61. case "$PROFILE" in
  62. emqx)
  63. true
  64. ;;
  65. emqx-enterprise)
  66. true
  67. ;;
  68. *)
  69. echo "ERROR: Failed to resolve build profile"
  70. exit 1
  71. ;;
  72. esac
  73. ;;
  74. esac
  75. echo "::set-output name=BUILD_PROFILE::$PROFILE"
  76. VSN="$(./pkg-vsn.sh "$PROFILE")"
  77. echo "Building $PROFILE image with tag $VSN (latest=$docker_latest)"
  78. echo "::set-output name=DOCKER_TAG_VERSION::$VSN"
  79. - name: get_all_deps
  80. run: |
  81. make -C source deps-all
  82. zip -ryq source.zip source/* source/.[^.]*
  83. - uses: actions/upload-artifact@v2
  84. with:
  85. name: source
  86. path: source.zip
  87. docker:
  88. runs-on: ${{ matrix.build_machine }}
  89. needs: prepare
  90. strategy:
  91. fail-fast: false
  92. matrix:
  93. arch:
  94. - amd64
  95. - arm64
  96. profile:
  97. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  98. build_elixir:
  99. - no_elixir
  100. registry:
  101. - 'docker.io'
  102. - 'public.ecr.aws'
  103. os:
  104. - [alpine3.15.1, "alpine:3.15.1", "deploy/docker/Dockerfile.alpine"]
  105. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  106. # NOTE: for docker, only support latest otp and elixir
  107. # versions, not a matrix
  108. otp:
  109. - 24.2.1-1 # update to latest
  110. elixir:
  111. - 1.13.4 # update to latest
  112. build_machine:
  113. - aws-arm64
  114. - ubuntu-20.04
  115. exclude:
  116. - registry: 'public.ecr.aws'
  117. profile: emqx-enterprise
  118. - arch: arm64
  119. build_machine: ubuntu-20.04
  120. - arch: amd64
  121. build_machine: aws-arm64
  122. include:
  123. - arch: amd64
  124. profile: emqx
  125. build_elixir: with_elixir
  126. registry: 'docker.io'
  127. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  128. otp: 24.2.1-1
  129. elixir: 1.13.4
  130. build_machine: ubuntu-20.04
  131. - arch: arm64
  132. profile: emqx
  133. build_elixir: with_elixir
  134. registry: 'docker.io'
  135. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  136. otp: 24.2.1-1
  137. elixir: 1.13.4
  138. build_machine: aws-arm64
  139. steps:
  140. - uses: AutoModality/action-clean@v1
  141. if: matrix.build_machine == 'aws-arm64'
  142. - uses: actions/download-artifact@v2
  143. with:
  144. name: source
  145. path: .
  146. - name: unzip source code
  147. run: unzip -q source.zip
  148. - uses: docker/setup-buildx-action@v1
  149. - name: Login for docker.
  150. uses: docker/login-action@v1
  151. if: matrix.registry == 'docker.io'
  152. with:
  153. username: ${{ secrets.DOCKER_HUB_USER }}
  154. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  155. - name: Login for AWS ECR
  156. uses: docker/login-action@v1
  157. if: matrix.registry == 'public.ecr.aws'
  158. with:
  159. registry: public.ecr.aws
  160. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  161. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  162. ecr: true
  163. - name: prepare for docker-action-parms
  164. id: pre-meta
  165. run: |
  166. emqx_name=${{ matrix.profile }}
  167. img_suffix=${{ matrix.arch }}
  168. img_labels="org.opencontainers.image.otp.version=${{ matrix.otp }}"
  169. if [ ${{ matrix.build_elixir }} = "with_elixir" ]; then
  170. emqx_name="emqx-elixir"
  171. img_suffix="elixir-${{ matrix.arch }}"
  172. img_labels="org.opencontainers.image.elixir.version=${{ matrix.elixir }}\n${img_labels}"
  173. fi
  174. if [[ ${{ matrix.os[0] }} =~ "alpine" ]]; then
  175. img_suffix="${img_suffix}-alpine"
  176. fi
  177. echo "::set-output name=emqx_name::${emqx_name}"
  178. echo "::set-output name=img_suffix::${img_suffix}"
  179. echo "::set-output name=img_labels::${img_labels}"
  180. # NOTE, Pls make sure this is identical as the one in job 'docker-push-multi-arch-manifest'
  181. - uses: docker/metadata-action@v3
  182. id: meta
  183. with:
  184. images: ${{ matrix.registry }}/${{ github.repository_owner }}/${{ matrix.profile }}
  185. flavor: |
  186. suffix=-${{ steps.pre-meta.outputs.img_suffix }}
  187. tags: |
  188. type=raw,value=${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  189. labels:
  190. ${{ steps.pre-meta.outputs.img_labels }}
  191. - uses: docker/build-push-action@v2
  192. with:
  193. push: ${{ needs.prepare.outputs.IS_EXACT_TAG }}
  194. pull: true
  195. no-cache: true
  196. platforms: linux/${{ matrix.arch }}
  197. tags: ${{ steps.meta.outputs.tags }}
  198. labels: ${{ steps.meta.outputs.labels }}
  199. build-args: |
  200. BUILD_FROM=ghcr.io/emqx/emqx-builder/5.0-17:${{ matrix.elixir }}-${{ matrix.otp }}-${{ matrix.os[0] }}
  201. RUN_FROM=${{ matrix.os[1] }}
  202. EMQX_NAME=${{ steps.pre-meta.outputs.emqx_name }}
  203. file: source/${{ matrix.os[2] }}
  204. context: source
  205. docker-push-multi-arch-manifest:
  206. # note, we only run on amd64
  207. if: needs.prepare.outputs.IS_EXACT_TAG == 'true'
  208. needs:
  209. - prepare
  210. - docker
  211. runs-on: ubuntu-latest
  212. strategy:
  213. fail-fast: false
  214. matrix:
  215. profile:
  216. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  217. build_elixir:
  218. - no_elixir
  219. os:
  220. - [alpine3.15.1, "alpine:3.15.1", "deploy/docker/Dockerfile.alpine"]
  221. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  222. # NOTE: for docker, only support latest otp version, not a matrix
  223. otp:
  224. - 24.2.1-1 # update to latest
  225. #
  226. elixir:
  227. - 1.13.4 # update to latest
  228. arch:
  229. - amd64
  230. #- arm64
  231. build_machine:
  232. - aws-arm64
  233. - ubuntu-20.04
  234. registry:
  235. - 'docker.io'
  236. - 'public.ecr.aws'
  237. exclude:
  238. - registry: 'public.ecr.aws'
  239. profile: emqx-enterprise
  240. - arch: arm64
  241. build_machine: ubuntu-20.04
  242. - arch: amd64
  243. build_machine: aws-arm64
  244. include:
  245. - arch: amd64
  246. profile: emqx
  247. build_elixir: with_elixir
  248. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  249. otp: 24.2.1-1
  250. elixir: 1.13.4
  251. build_machine: ubuntu-20.04
  252. registry: docker.io
  253. steps:
  254. - uses: actions/download-artifact@v2
  255. with:
  256. name: source
  257. path: .
  258. - name: unzip source code
  259. run: unzip -q source.zip
  260. - uses: docker/login-action@v1
  261. if: matrix.registry == 'docker.io'
  262. with:
  263. username: ${{ secrets.DOCKER_HUB_USER }}
  264. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  265. - uses: docker/login-action@v1
  266. if: matrix.registry == 'public.ecr.aws'
  267. with:
  268. registry: public.ecr.aws
  269. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  270. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  271. ecr: true
  272. - name: prepare for docker-action-parms
  273. id: pre-meta
  274. run: |
  275. emqx_name=${{ matrix.profile }}
  276. img_suffix=${{ matrix.arch }}
  277. img_labels="org.opencontainers.image.otp.version=${{ matrix.otp }}"
  278. if [ ${{ matrix.build_elixir }} = 'with_elixir' ]; then
  279. emqx_name="emqx-elixir"
  280. img_suffix="elixir-${{ matrix.arch }}"
  281. img_labels="org.opencontainers.image.elixir.version=${{ matrix.elixir }}\n$img_labels"
  282. fi
  283. if [[ ${{ matrix.os[0] }} =~ "alpine" ]]; then
  284. img_suffix="${img_suffix}-alpine"
  285. fi
  286. echo "::set-output name=img::${img}"
  287. echo "::set-output name=emqx_name::${emqx_name}"
  288. echo "::set-output name=img_suffix::${img_suffix}"
  289. echo "::set-output name=img_labels::${img_labels}"
  290. # NOTE, Pls make sure this is identical as the one in job 'docker'
  291. - uses: docker/metadata-action@v3
  292. id: meta
  293. with:
  294. images: ${{ matrix.registry }}/${{ github.repository_owner }}/${{ matrix.profile }}
  295. flavor: |
  296. suffix=-${{ steps.pre-meta.outputs.img_suffix }}
  297. tags: |
  298. type=raw,value=${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  299. labels:
  300. ${{ steps.pre-meta.outputs.img_labels }}
  301. - name: update manifest for multiarch image
  302. if: needs.prepare.outputs.IS_EXACT_TAG == 'true'
  303. working-directory: source
  304. run: |
  305. if [ ${{ matrix.build_elixir }} = 'with_elixir' ]; then
  306. is_latest=false
  307. else
  308. is_latest="${{ needs.prepare.outputs.IS_DOCKER_LATEST }}"
  309. fi
  310. scripts/docker-create-push-manifests.sh "${{ steps.meta.outputs.tags }}" "$is_latest"