2020-10-10 20:44:56 +00:00
|
|
|
#!/usr/bin/env bash
|
2023-10-20 12:10:03 +00:00
|
|
|
# ./bin/build_docker.sh dev 'linux/arm/v7'
|
2020-10-10 20:44:56 +00: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
|
2024-10-05 08:17:23 +00:00
|
|
|
IFS=$' '
|
2020-10-10 20:44:56 +00:00
|
|
|
|
|
|
|
REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd .. && pwd )"
|
2023-03-13 12:55:28 +00:00
|
|
|
cd "$REPO_DIR"
|
2023-10-31 10:06:02 +00:00
|
|
|
|
2023-03-13 12:55:28 +00:00
|
|
|
which docker > /dev/null || exit 1
|
2023-10-31 10:06:02 +00:00
|
|
|
which jq > /dev/null || exit 1
|
|
|
|
# which pdm > /dev/null || exit 1
|
|
|
|
|
2024-10-05 10:56:20 +00:00
|
|
|
declare -a TAG_NAMES=("$@")
|
2024-10-05 08:17:23 +00:00
|
|
|
BRANCH_NAME="${1:-$(git rev-parse --abbrev-ref HEAD)}"
|
2024-10-05 10:51:47 +00:00
|
|
|
VERSION="$(grep '^version = ' "${REPO_DIR}/pyproject.toml" | awk -F'"' '{print $2}')"
|
2023-12-18 05:51:04 +00:00
|
|
|
GIT_SHA=sha-"$(git rev-parse --short HEAD)"
|
2024-10-05 08:17:23 +00:00
|
|
|
SELECTED_PLATFORMS="linux/amd64,linux/arm64"
|
|
|
|
|
|
|
|
# if not already in TAG_NAMES, add GIT_SHA and BRANCH_NAME
|
|
|
|
if ! echo "${TAG_NAMES[@]}" | grep -q "$GIT_SHA"; then
|
|
|
|
TAG_NAMES+=("$GIT_SHA")
|
|
|
|
fi
|
|
|
|
if ! echo "${TAG_NAMES[@]}" | grep -q "$BRANCH_NAME"; then
|
|
|
|
TAG_NAMES+=("$BRANCH_NAME")
|
|
|
|
fi
|
|
|
|
if ! echo "${TAG_NAMES[@]}" | grep -q "$VERSION"; then
|
|
|
|
TAG_NAMES+=("$VERSION")
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "[+] Building Docker image for $SELECTED_PLATFORMS: branch=$BRANCH_NAME version=$VERSION tags=${TAG_NAMES[*]}"
|
|
|
|
|
|
|
|
declare -a FULL_TAG_NAMES
|
|
|
|
# for each tag in TAG_NAMES, add archivebox/archivebox:tag and nikisweeting/archivebox:tag to FULL_TAG_NAMES
|
|
|
|
for TAG_NAME in "${TAG_NAMES[@]}"; do
|
|
|
|
[[ "$TAG_NAME" == "" ]] && continue
|
|
|
|
FULL_TAG_NAMES+=("-t archivebox/archivebox:$TAG_NAME")
|
|
|
|
FULL_TAG_NAMES+=("-t nikisweeting/archivebox:$TAG_NAME")
|
|
|
|
FULL_TAG_NAMES+=("-t ghcr.io/archivebox/archivebox:$TAG_NAME")
|
|
|
|
done
|
|
|
|
echo "${FULL_TAG_NAMES[@]}"
|
2023-03-13 12:55:28 +00:00
|
|
|
|
|
|
|
function check_platforms() {
|
|
|
|
INSTALLED_PLATFORMS="$(docker buildx inspect | grep 'Platforms:' )"
|
|
|
|
|
2023-11-01 02:11:14 +00:00
|
|
|
for REQUIRED_PLATFORM in ${SELECTED_PLATFORMS//,/$IFS}; do
|
2023-03-13 12:55:28 +00:00
|
|
|
echo "[+] Checking for: $REQUIRED_PLATFORM..."
|
|
|
|
if ! (echo "$INSTALLED_PLATFORMS" | grep -q "$REQUIRED_PLATFORM"); then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-10-20 12:10:03 +00:00
|
|
|
function remove_builder() {
|
|
|
|
# remove existing xbuilder
|
|
|
|
docker buildx stop xbuilder || true
|
|
|
|
docker buildx rm xbuilder || true
|
|
|
|
}
|
|
|
|
|
2023-03-13 12:55:28 +00:00
|
|
|
function create_builder() {
|
2023-10-20 12:10:03 +00:00
|
|
|
docker buildx use xbuilder && return 0
|
2023-11-01 02:11:14 +00:00
|
|
|
echo "[+] Creating new xbuilder for: $SELECTED_PLATFORMS"
|
2023-03-13 12:55:28 +00:00
|
|
|
echo
|
2023-05-04 08:43:47 +00:00
|
|
|
docker pull 'moby/buildkit:buildx-stable-1'
|
2023-03-13 12:55:28 +00:00
|
|
|
|
|
|
|
# Switch to buildx builder if already present / previously created
|
2023-11-01 02:11:14 +00:00
|
|
|
docker buildx create --name xbuilder --driver docker-container --bootstrap --use --platform "$SELECTED_PLATFORMS" || true
|
2023-03-13 12:55:28 +00:00
|
|
|
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-10 20:44:56 +00:00
|
|
|
|
2023-10-20 12:10:03 +00:00
|
|
|
remove_builder
|
2023-03-13 12:55:28 +00:00
|
|
|
create_builder
|
|
|
|
}
|
2023-03-13 11:57:20 +00:00
|
|
|
|
2023-03-13 12:55:28 +00:00
|
|
|
# Check if docker is ready for cross-plaform builds, if not, recreate builder
|
2024-06-03 08:13:17 +00:00
|
|
|
docker buildx use xbuilder >/dev/null 2>&1 || create_builder
|
2023-03-13 12:55:28 +00:00
|
|
|
check_platforms || (recreate_builder && check_platforms) || exit 1
|
2023-03-13 11:57:20 +00:00
|
|
|
|
|
|
|
|
2024-04-24 00:43:01 +00:00
|
|
|
# Make sure pyproject.toml, pdm{.dev}.lock, requirements{-dev}.txt, package{-lock}.json are all up-to-date
|
2024-10-05 08:17:23 +00:00
|
|
|
# echo "[!] Make sure you've run ./bin/lock_pkgs.sh recently!"
|
|
|
|
bash ./bin/lock_pkgs.sh
|
2023-10-31 10:06:02 +00:00
|
|
|
|
2023-05-04 08:43:34 +00:00
|
|
|
|
2021-02-01 08:09:31 +00:00
|
|
|
echo "[+] Building archivebox:$VERSION docker image..."
|
2023-10-20 11:08:38 +00:00
|
|
|
# docker builder prune
|
|
|
|
# docker build . --no-cache -t archivebox-dev \
|
2023-11-01 02:11:14 +00:00
|
|
|
# replace --load with --push to deploy
|
2024-10-05 08:17:23 +00:00
|
|
|
# shellcheck disable=SC2068
|
|
|
|
docker buildx build --platform "$SELECTED_PLATFORMS" --load . ${FULL_TAG_NAMES[@]}
|