1
0
Fork 0
mirror of synced 2024-05-17 19:02:33 +12:00
ArchiveBox/bin/build_docker.sh

100 lines
3.6 KiB
Bash
Raw Normal View History

2020-10-11 09:44:56 +13:00
#!/usr/bin/env bash
2023-10-21 01:10:03 +13:00
# ./bin/build_docker.sh dev 'linux/arm/v7'
2020-10-11 09:44:56 +13:00
### Bash Environment Setup
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# set -o xtrace
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
IFS=$'\n'
REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd .. && pwd )"
cd "$REPO_DIR"
2023-10-31 23:06:02 +13:00
which docker > /dev/null || exit 1
2023-10-31 23:06:02 +13:00
which jq > /dev/null || exit 1
# which pdm > /dev/null || exit 1
SUPPORTED_PLATFORMS="linux/amd64,linux/arm64"
2023-10-21 01:10:03 +13:00
TAG_NAME="${1:-$(git rev-parse --abbrev-ref HEAD)}"
2020-10-11 09:44:56 +13:00
VERSION="$(jq -r '.version' < "$REPO_DIR/package.json")"
2021-01-11 13:50:57 +13:00
SHORT_VERSION="$(echo "$VERSION" | perl -pe 's/(\d+)\.(\d+)\.(\d+)/$1.$2/g')"
2023-12-18 18:51:04 +13:00
GIT_SHA=sha-"$(git rev-parse --short HEAD)"
2023-11-01 15:11:14 +13:00
SELECTED_PLATFORMS="${2:-$SUPPORTED_PLATFORMS}"
2023-10-21 01:10:03 +13:00
2023-11-01 15:11:14 +13:00
echo "[+] Building Docker image: tag=$TAG_NAME version=$SHORT_VERSION arch=$SELECTED_PLATFORMS"
function check_platforms() {
INSTALLED_PLATFORMS="$(docker buildx inspect | grep 'Platforms:' )"
2023-11-01 15:11:14 +13:00
for REQUIRED_PLATFORM in ${SELECTED_PLATFORMS//,/$IFS}; do
echo "[+] Checking for: $REQUIRED_PLATFORM..."
if ! (echo "$INSTALLED_PLATFORMS" | grep -q "$REQUIRED_PLATFORM"); then
return 1
fi
done
echo
return 0
}
2023-10-21 01:10:03 +13:00
function remove_builder() {
# remove existing xbuilder
docker buildx stop xbuilder || true
docker buildx rm xbuilder || true
}
function create_builder() {
2023-10-21 01:10:03 +13:00
docker buildx use xbuilder && return 0
2023-11-01 15:11:14 +13:00
echo "[+] Creating new xbuilder for: $SELECTED_PLATFORMS"
echo
2023-05-04 20:43:47 +12:00
docker pull 'moby/buildkit:buildx-stable-1'
# Switch to buildx builder if already present / previously created
2023-11-01 15:11:14 +13:00
docker buildx create --name xbuilder --driver docker-container --bootstrap --use --platform "$SELECTED_PLATFORMS" || true
docker buildx inspect --bootstrap || true
}
function recreate_builder() {
# Install QEMU binaries for cross-platform building if not installed
docker run --privileged --rm 'tonistiigi/binfmt' --install all
2020-10-11 09:44:56 +13:00
2023-10-21 01:10:03 +13:00
remove_builder
create_builder
}
# Check if docker is ready for cross-plaform builds, if not, recreate builder
2023-10-21 01:10:03 +13:00
docker buildx use xbuilder 2>&1 >/dev/null || create_builder
check_platforms || (recreate_builder && check_platforms) || exit 1
# Make sure pyproject.toml, pdm{.dev}.lock, requirements{-dev}.txt, package{-lock}.json are all up-to-date
bash ./bin/lock_pkgs.sh
2023-10-31 23:06:02 +13:00
2023-05-04 20:43:34 +12:00
2021-02-01 21:09:31 +13:00
echo "[+] Building archivebox:$VERSION docker image..."
2023-10-21 00:08:38 +13:00
# docker builder prune
# docker build . --no-cache -t archivebox-dev \
2023-11-01 15:11:14 +13:00
# replace --load with --push to deploy
docker buildx build --platform "$SELECTED_PLATFORMS" --load . \
# -t archivebox/archivebox \
2023-11-01 15:11:14 +13:00
-t archivebox/archivebox:$TAG_NAME \
# -t archivebox/archivebox:$VERSION \
# -t archivebox/archivebox:$SHORT_VERSION \
2023-12-18 18:51:04 +13:00
-t archivebox/archivebox:$GIT_SHA \
# -t archivebox/archivebox:latest \
# -t nikisweeting/archivebox \
2023-11-01 15:11:14 +13:00
-t nikisweeting/archivebox:$TAG_NAME \
# -t nikisweeting/archivebox:$VERSION \
# -t nikisweeting/archivebox:$SHORT_VERSION \
2023-12-18 18:51:04 +13:00
-t nikisweeting/archivebox:$GIT_SHA \
# -t nikisweeting/archivebox:latest \
2023-11-01 15:11:14 +13:00
-t ghcr.io/archivebox/archivebox/archivebox:$TAG_NAME \
# -t ghcr.io/archivebox/archivebox/archivebox:$VERSION \
# -t ghcr.io/archivebox/archivebox/archivebox:$SHORT_VERSION \
2023-12-18 18:51:04 +13:00
-t ghcr.io/archivebox/archivebox/archivebox:$GIT_SHA \
# -t ghcr.io/archivebox/archivebox/archivebox:latest