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
|
|
|
|
IFS=$'\n'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-10-31 13:28:11 +00:00
|
|
|
SUPPORTED_PLATFORMS="linux/amd64,linux/arm64,linux/arm/v7"
|
2023-03-13 12:55:28 +00:00
|
|
|
|
2023-10-20 12:10:03 +00:00
|
|
|
TAG_NAME="${1:-$(git rev-parse --abbrev-ref HEAD)}"
|
2020-10-10 20:44:56 +00:00
|
|
|
VERSION="$(jq -r '.version' < "$REPO_DIR/package.json")"
|
2021-01-11 00:50:57 +00:00
|
|
|
SHORT_VERSION="$(echo "$VERSION" | perl -pe 's/(\d+)\.(\d+)\.(\d+)/$1.$2/g')"
|
2023-12-18 05:51:04 +00:00
|
|
|
GIT_SHA=sha-"$(git rev-parse --short HEAD)"
|
2023-11-01 02:11:14 +00:00
|
|
|
SELECTED_PLATFORMS="${2:-$SUPPORTED_PLATFORMS}"
|
2023-10-20 12:10:03 +00:00
|
|
|
|
2023-11-01 02:11:14 +00:00
|
|
|
echo "[+] Building Docker image: tag=$TAG_NAME version=$SHORT_VERSION arch=$SELECTED_PLATFORMS"
|
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
|
2023-10-20 12:10:03 +00:00
|
|
|
docker buildx use xbuilder 2>&1 >/dev/null || 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
|
|
|
|
|
|
|
|
2023-10-31 10:06:02 +00:00
|
|
|
# Build python package lists
|
|
|
|
echo "[+] Generating requirements.txt and pdm.lock from pyproject.toml..."
|
|
|
|
pdm lock --group=':all' --strategy="cross_platform" --production
|
|
|
|
pdm export --group=':all' --production --without-hashes -o requirements.txt
|
|
|
|
|
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
|
|
|
|
docker buildx build --platform "$SELECTED_PLATFORMS" --load . \
|
|
|
|
-t archivebox/archivebox \
|
|
|
|
-t archivebox/archivebox:$TAG_NAME \
|
|
|
|
-t archivebox/archivebox:$VERSION \
|
|
|
|
-t archivebox/archivebox:$SHORT_VERSION \
|
2023-12-18 05:51:04 +00:00
|
|
|
-t archivebox/archivebox:$GIT_SHA \
|
2023-11-01 02:11:14 +00:00
|
|
|
-t archivebox/archivebox:latest \
|
|
|
|
-t nikisweeting/archivebox \
|
|
|
|
-t nikisweeting/archivebox:$TAG_NAME \
|
|
|
|
-t nikisweeting/archivebox:$VERSION \
|
|
|
|
-t nikisweeting/archivebox:$SHORT_VERSION \
|
2023-12-18 05:51:04 +00:00
|
|
|
-t nikisweeting/archivebox:$GIT_SHA \
|
2023-11-01 02:11:14 +00:00
|
|
|
-t nikisweeting/archivebox:latest \
|
|
|
|
-t ghcr.io/archivebox/archivebox/archivebox:$TAG_NAME \
|
|
|
|
-t ghcr.io/archivebox/archivebox/archivebox:$VERSION \
|
2023-12-18 05:51:04 +00:00
|
|
|
-t ghcr.io/archivebox/archivebox/archivebox:$SHORT_VERSION \
|
|
|
|
-t ghcr.io/archivebox/archivebox/archivebox:$GIT_SHA \
|
|
|
|
-t ghcr.io/archivebox/archivebox/archivebox:latest
|