buildx.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. ## This script helps to run docker buildx to build cross-arch/platform packages (linux only)
  3. ## It mounts (not copy) host directory to a cross-arch/platform builder container
  4. ## Make sure the source dir (specified by --src_dir option) is clean before running this script
  5. ## NOTE: it requires $USER in docker group
  6. ## i.e. will not work if docker command has to be executed with sudo
  7. ## example:
  8. ## ./scripts/buildx.sh --profile emqx --pkgtype zip --arch arm64 --builder ghcr.io/emqx/emqx-builder/4.4-4:24.1.5-3-debian10
  9. set -euo pipefail
  10. help() {
  11. echo
  12. echo "-h|--help: To display this usage information"
  13. echo "--profile <PROFILE>: EMQ X profile to build, e.g. emqx, emqx-edge"
  14. echo "--pkgtype zip|pkg: Specify which package to build, zip for .zip and pkg for .rpm or .deb"
  15. echo "--arch amd64|arm64: Target arch to build the EMQ X package for"
  16. echo "--src_dir <SRC_DIR>: EMQ X source ode in this dir, default to PWD"
  17. echo "--builder <BUILDER>: Builder image to pull"
  18. echo " E.g. ghcr.io/emqx/emqx-builder/4.4-4:24.1.5-3-debian10"
  19. }
  20. while [ "$#" -gt 0 ]; do
  21. case $1 in
  22. -h|--help)
  23. help
  24. exit 0
  25. ;;
  26. --src_dir)
  27. SRC_DIR="$2"
  28. shift 2
  29. ;;
  30. --profile)
  31. PROFILE="$2"
  32. shift 2
  33. ;;
  34. --pkgtype)
  35. PKGTYPE="$2"
  36. shift 2
  37. ;;
  38. --builder)
  39. BUILDER="$2"
  40. shift 2
  41. ;;
  42. --arch)
  43. ARCH="$2"
  44. shift 2
  45. ;;
  46. *)
  47. echo "WARN: Unknown arg (ignored): $1"
  48. shift
  49. continue
  50. ;;
  51. esac
  52. done
  53. if [ -z "${PROFILE:-}" ] || [ -z "${PKGTYPE:-}" ] || [ -z "${BUILDER:-}" ] || [ -z "${ARCH:-}" ]; then
  54. help
  55. exit 1
  56. fi
  57. if [ "$PKGTYPE" != 'zip' ] && [ "$PKGTYPE" != 'pkg' ]; then
  58. echo "Bad --pkgtype option, should be zip or pkg"
  59. exit 1
  60. fi
  61. cd "${SRC_DIR:-.}"
  62. PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh)}"
  63. OTP_VSN_SYSTEM=$(echo "$BUILDER" | cut -d ':' -f2)
  64. PKG_NAME="${PROFILE}-${PKG_VSN}-otp${OTP_VSN_SYSTEM}-${ARCH}"
  65. docker info
  66. docker run --rm --privileged tonistiigi/binfmt:latest --install "${ARCH}"
  67. docker run -i --rm \
  68. -v "$(pwd)":/emqx \
  69. --workdir /emqx \
  70. --platform="linux/$ARCH" \
  71. -e EMQX_NAME="$PROFILE" \
  72. "$BUILDER" \
  73. bash -euc "make ${PROFILE}-${PKGTYPE} && .ci/build_packages/tests.sh $PKG_NAME $PKGTYPE"