build_docker.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env bash
  2. # ./bin/build_docker.sh dev 'linux/arm/v7'
  3. ### Bash Environment Setup
  4. # http://redsymbol.net/articles/unofficial-bash-strict-mode/
  5. # https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
  6. # set -o xtrace
  7. set -o errexit
  8. set -o errtrace
  9. set -o nounset
  10. set -o pipefail
  11. IFS=$' '
  12. REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd .. && pwd )"
  13. cd "$REPO_DIR"
  14. which docker > /dev/null || exit 1
  15. which jq > /dev/null || exit 1
  16. # which pdm > /dev/null || exit 1
  17. declare -a TAG_NAMES=("$@")
  18. BRANCH_NAME="${1:-$(git rev-parse --abbrev-ref HEAD)}"
  19. VERSION="$(grep '^version = ' "${REPO_DIR}/pyproject.toml" | awk -F'"' '{print $2}')"
  20. GIT_SHA=sha-"$(git rev-parse --short HEAD)"
  21. SELECTED_PLATFORMS="linux/amd64,linux/arm64"
  22. # if not already in TAG_NAMES, add GIT_SHA and BRANCH_NAME
  23. if ! echo "${TAG_NAMES[@]}" | grep -q "$GIT_SHA"; then
  24. TAG_NAMES+=("$GIT_SHA")
  25. fi
  26. if ! echo "${TAG_NAMES[@]}" | grep -q "$BRANCH_NAME"; then
  27. TAG_NAMES+=("$BRANCH_NAME")
  28. fi
  29. if ! echo "${TAG_NAMES[@]}" | grep -q "$VERSION"; then
  30. TAG_NAMES+=("$VERSION")
  31. fi
  32. echo "[+] Building Docker image for $SELECTED_PLATFORMS: branch=$BRANCH_NAME version=$VERSION tags=${TAG_NAMES[*]}"
  33. declare -a FULL_TAG_NAMES
  34. # for each tag in TAG_NAMES, add archivebox/archivebox:tag and nikisweeting/archivebox:tag to FULL_TAG_NAMES
  35. for TAG_NAME in "${TAG_NAMES[@]}"; do
  36. [[ "$TAG_NAME" == "" ]] && continue
  37. FULL_TAG_NAMES+=("-t archivebox/archivebox:$TAG_NAME")
  38. FULL_TAG_NAMES+=("-t nikisweeting/archivebox:$TAG_NAME")
  39. FULL_TAG_NAMES+=("-t ghcr.io/archivebox/archivebox:$TAG_NAME")
  40. done
  41. echo "${FULL_TAG_NAMES[@]}"
  42. function check_platforms() {
  43. INSTALLED_PLATFORMS="$(docker buildx inspect | grep 'Platforms:' )"
  44. for REQUIRED_PLATFORM in ${SELECTED_PLATFORMS//,/$IFS}; do
  45. echo "[+] Checking for: $REQUIRED_PLATFORM..."
  46. if ! (echo "$INSTALLED_PLATFORMS" | grep -q "$REQUIRED_PLATFORM"); then
  47. return 1
  48. fi
  49. done
  50. echo
  51. return 0
  52. }
  53. function remove_builder() {
  54. # remove existing xbuilder
  55. docker buildx stop xbuilder || true
  56. docker buildx rm xbuilder || true
  57. }
  58. function create_builder() {
  59. docker buildx use xbuilder && return 0
  60. echo "[+] Creating new xbuilder for: $SELECTED_PLATFORMS"
  61. echo
  62. docker pull 'moby/buildkit:buildx-stable-1'
  63. # Switch to buildx builder if already present / previously created
  64. docker buildx create --name xbuilder --driver docker-container --bootstrap --use --platform "$SELECTED_PLATFORMS" || true
  65. docker buildx inspect --bootstrap || true
  66. }
  67. function recreate_builder() {
  68. # Install QEMU binaries for cross-platform building if not installed
  69. docker run --privileged --rm 'tonistiigi/binfmt' --install all
  70. remove_builder
  71. create_builder
  72. }
  73. # Check if docker is ready for cross-plaform builds, if not, recreate builder
  74. docker buildx use xbuilder >/dev/null 2>&1 || create_builder
  75. check_platforms || (recreate_builder && check_platforms) || exit 1
  76. # Make sure pyproject.toml, pdm{.dev}.lock, requirements{-dev}.txt, package{-lock}.json are all up-to-date
  77. # echo "[!] Make sure you've run ./bin/lock_pkgs.sh recently!"
  78. bash ./bin/lock_pkgs.sh
  79. echo "[+] Building archivebox:$VERSION docker image..."
  80. # docker builder prune
  81. # docker build . --no-cache -t archivebox-dev \
  82. # replace --load with --push to deploy
  83. # shellcheck disable=SC2068
  84. docker buildx build --platform "$SELECTED_PLATFORMS" --load . ${FULL_TAG_NAMES[@]}