build_docker.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 its mirrors 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") # ArchiveBox official Docker repo
  38. FULL_TAG_NAMES+=("-t ghcr.io/archivebox/archivebox:$TAG_NAME") # Github Container Repo mirror
  39. done
  40. echo "${FULL_TAG_NAMES[@]}"
  41. function check_platforms() {
  42. INSTALLED_PLATFORMS="$(docker buildx inspect | grep 'Platforms:' )"
  43. for REQUIRED_PLATFORM in ${SELECTED_PLATFORMS//,/$IFS}; do
  44. echo "[+] Checking for: $REQUIRED_PLATFORM..."
  45. if ! (echo "$INSTALLED_PLATFORMS" | grep -q "$REQUIRED_PLATFORM"); then
  46. return 1
  47. fi
  48. done
  49. echo
  50. return 0
  51. }
  52. function remove_builder() {
  53. # remove existing xbuilder
  54. docker buildx stop xbuilder || true
  55. docker buildx rm xbuilder || true
  56. }
  57. function create_builder() {
  58. docker buildx use xbuilder && return 0
  59. echo "[+] Creating new xbuilder for: $SELECTED_PLATFORMS"
  60. echo
  61. docker pull 'moby/buildkit:buildx-stable-1'
  62. # Switch to buildx builder if already present / previously created
  63. docker buildx create --name xbuilder --driver docker-container --bootstrap --use --platform "$SELECTED_PLATFORMS" || true
  64. docker buildx inspect --bootstrap || true
  65. }
  66. function recreate_builder() {
  67. # Install QEMU binaries for cross-platform building if not installed
  68. docker run --privileged --rm 'tonistiigi/binfmt' --install all
  69. remove_builder
  70. create_builder
  71. }
  72. # Check if docker is ready for cross-plaform builds, if not, recreate builder
  73. docker buildx use xbuilder >/dev/null 2>&1 || create_builder
  74. check_platforms || (recreate_builder && check_platforms) || exit 1
  75. # Make sure pyproject.toml, pdm{.dev}.lock, requirements{-dev}.txt, package{-lock}.json are all up-to-date
  76. # echo "[!] Make sure you've run ./bin/lock_pkgs.sh recently!"
  77. bash ./bin/lock_pkgs.sh
  78. echo "[+] Building archivebox:$VERSION docker image..."
  79. # docker builder prune
  80. # docker build . --no-cache -t archivebox-dev \
  81. # replace --load with --push to deploy
  82. # shellcheck disable=SC2068
  83. docker buildx build --platform "$SELECTED_PLATFORMS" --load . ${FULL_TAG_NAMES[@]}