build_and_push_docker_images.yaml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. - arch: arm64
  117. build_machine: ubuntu-20.04
  118. - arch: amd64
  119. build_machine: aws-arm64
  120. include:
  121. - arch: amd64
  122. profile: emqx
  123. build_elixir: with_elixir
  124. registry: 'docker.io'
  125. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  126. otp: 24.2.1-1
  127. elixir: 1.13.4
  128. build_machine: ubuntu-20.04
  129. - arch: arm64
  130. profile: emqx
  131. build_elixir: with_elixir
  132. registry: 'docker.io'
  133. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  134. otp: 24.2.1-1
  135. elixir: 1.13.4
  136. build_machine: aws-arm64
  137. steps:
  138. - uses: AutoModality/action-clean@v1
  139. if: matrix.build_machine == 'aws-arm64'
  140. - uses: actions/download-artifact@v2
  141. with:
  142. name: source
  143. path: .
  144. - name: unzip source code
  145. run: unzip -q source.zip
  146. - uses: docker/setup-buildx-action@v1
  147. - name: Login for docker.
  148. uses: docker/login-action@v1
  149. if: matrix.registry == 'docker.io'
  150. with:
  151. username: ${{ secrets.DOCKER_HUB_USER }}
  152. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  153. - name: Login for AWS ECR
  154. uses: docker/login-action@v1
  155. if: matrix.registry == 'public.ecr.aws'
  156. with:
  157. registry: public.ecr.aws
  158. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  159. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  160. ecr: true
  161. - name: prepare for docker-action-parms
  162. id: pre-meta
  163. run: |
  164. emqx_name=${{ matrix.profile }}
  165. img_suffix=${{ matrix.arch }}
  166. img_labels="org.opencontainers.image.otp.version=${{ matrix.otp }}"
  167. if [ ${{ matrix.build_elixir }} = "with_elixir" ]; then
  168. emqx_name="emqx-elixir"
  169. img_suffix="elixir-${{ matrix.arch }}"
  170. img_labels="org.opencontainers.image.elixir.version=${{ matrix.elixir }}\n${img_labels}"
  171. fi
  172. if [[ ${{ matrix.os[0] }} =~ "alpine" ]]; then
  173. img_suffix="${img_suffix}-alpine"
  174. fi
  175. echo "::set-output name=emqx_name::${emqx_name}"
  176. echo "::set-output name=img_suffix::${img_suffix}"
  177. echo "::set-output name=img_labels::${img_labels}"
  178. # NOTE, Pls make sure this is identical as the one in job 'docker-push-multi-arch-manifest'
  179. - uses: docker/metadata-action@v3
  180. id: meta
  181. with:
  182. images: ${{ matrix.registry }}/${{ github.repository_owner }}/${{ matrix.profile }}
  183. flavor: |
  184. suffix=-${{ steps.pre-meta.outputs.img_suffix }}
  185. tags: |
  186. type=raw,value=${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  187. labels:
  188. ${{ steps.pre-meta.outputs.img_labels }}
  189. - uses: docker/build-push-action@v2
  190. with:
  191. push: ${{ needs.prepare.outputs.IS_EXACT_TAG }}
  192. pull: true
  193. no-cache: true
  194. platforms: linux/${{ matrix.arch }}
  195. tags: ${{ steps.meta.outputs.tags }}
  196. labels: ${{ steps.meta.outputs.labels }}
  197. build-args: |
  198. BUILD_FROM=ghcr.io/emqx/emqx-builder/5.0-17:${{ matrix.elixir }}-${{ matrix.otp }}-${{ matrix.os[0] }}
  199. RUN_FROM=${{ matrix.os[1] }}
  200. EMQX_NAME=${{ steps.pre-meta.outputs.emqx_name }}
  201. file: source/${{ matrix.os[2] }}
  202. context: source
  203. docker-push-multi-arch-manifest:
  204. # note, we only run on amd64
  205. if: ${{ needs.prepare.outputs.IS_EXACT_TAG }}
  206. needs:
  207. - prepare
  208. - docker
  209. runs-on: ubuntu-latest
  210. strategy:
  211. fail-fast: false
  212. matrix:
  213. profile:
  214. - ${{ needs.prepare.outputs.BUILD_PROFILE }}
  215. build_elixir:
  216. - no_elixir
  217. os:
  218. - [alpine3.15.1, "alpine:3.15.1", "deploy/docker/Dockerfile.alpine"]
  219. - [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  220. # NOTE: for docker, only support latest otp version, not a matrix
  221. otp:
  222. - 24.2.1-1 # update to latest
  223. #
  224. elixir:
  225. - 1.13.4 # update to latest
  226. arch:
  227. - amd64
  228. #- arm64
  229. build_machine:
  230. - aws-arm64
  231. - ubuntu-20.04
  232. registry:
  233. - 'docker.io'
  234. - 'public.ecr.aws'
  235. exclude:
  236. - arch: arm64
  237. build_machine: ubuntu-20.04
  238. - arch: amd64
  239. build_machine: aws-arm64
  240. include:
  241. - arch: amd64
  242. profile: emqx
  243. build_elixir: with_elixir
  244. os: [debian11, "debian:11-slim", "deploy/docker/Dockerfile"]
  245. otp: 24.2.1-1
  246. elixir: 1.13.4
  247. build_machine: ubuntu-20.04
  248. registry: docker.io
  249. steps:
  250. - uses: actions/download-artifact@v2
  251. with:
  252. name: source
  253. path: .
  254. - name: unzip source code
  255. run: unzip -q source.zip
  256. - uses: docker/login-action@v1
  257. if: matrix.registry == 'docker.io'
  258. with:
  259. username: ${{ secrets.DOCKER_HUB_USER }}
  260. password: ${{ secrets.DOCKER_HUB_TOKEN }}
  261. - uses: docker/login-action@v1
  262. if: matrix.registry == 'public.ecr.aws'
  263. with:
  264. registry: public.ecr.aws
  265. username: ${{ secrets.AWS_ACCESS_KEY_ID }}
  266. password: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  267. ecr: true
  268. - name: prepare for docker-action-parms
  269. id: pre-meta
  270. run: |
  271. emqx_name=${{ matrix.profile }}
  272. img_suffix=${{ matrix.arch }}
  273. img_labels="org.opencontainers.image.otp.version=${{ matrix.otp }}"
  274. if [ ${{ matrix.build_elixir }} = 'with_elixir' ]; then
  275. emqx_name="emqx-elixir"
  276. img_suffix="elixir-${{ matrix.arch }}"
  277. img_labels="org.opencontainers.image.elixir.version=${{ matrix.elixir }}\n$img_labels"
  278. fi
  279. if [[ ${{ matrix.os[0] }} =~ "alpine" ]]; then
  280. img_suffix="${img_suffix}-alpine"
  281. fi
  282. echo "::set-output name=img::${img}"
  283. echo "::set-output name=emqx_name::${emqx_name}"
  284. echo "::set-output name=img_suffix::${img_suffix}"
  285. echo "::set-output name=img_labels::${img_labels}"
  286. # NOTE, Pls make sure this is identical as the one in job 'docker'
  287. - uses: docker/metadata-action@v3
  288. id: meta
  289. with:
  290. images: ${{ matrix.registry }}/${{ github.repository_owner }}/${{ matrix.profile }}
  291. flavor: |
  292. suffix=-${{ steps.pre-meta.outputs.img_suffix }}
  293. tags: |
  294. type=raw,value=${{ needs.prepare.outputs.DOCKER_TAG_VERSION }}
  295. labels:
  296. ${{ steps.pre-meta.outputs.img_labels }}
  297. - name: update manifest for multiarch image
  298. if: ${{ needs.prepare.outputs.IS_EXACT_TAG }}
  299. working-directory: source
  300. run: |
  301. if [ ${{ matrix.build_elixir }} = 'with_elixir' ]; then
  302. is_latest=false
  303. else
  304. is_latest="${{ needs.prepare.outputs.IS_DOCKER_LATEST }}"
  305. fi
  306. scripts/docker-create-push-manifests.sh "${{ steps.meta.outputs.tags }}" "$is_latest"