瀏覽代碼

recreate dockerx builder if missing platforms

Nick Sweeting 2 年之前
父節點
當前提交
cc53eceda2
共有 1 個文件被更改,包括 45 次插入10 次删除
  1. 45 10
      bin/build_docker.sh

+ 45 - 10
bin/build_docker.sh

@@ -11,27 +11,62 @@ set -o pipefail
 IFS=$'\n'
 IFS=$'\n'
 
 
 REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd .. && pwd )"
 REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && cd .. && pwd )"
+cd "$REPO_DIR"
+which docker > /dev/null || exit 1
+
+
+TAG_NAME="dev"
 VERSION="$(jq -r '.version' < "$REPO_DIR/package.json")"
 VERSION="$(jq -r '.version' < "$REPO_DIR/package.json")"
 SHORT_VERSION="$(echo "$VERSION" | perl -pe 's/(\d+)\.(\d+)\.(\d+)/$1.$2/g')"
 SHORT_VERSION="$(echo "$VERSION" | perl -pe 's/(\d+)\.(\d+)\.(\d+)/$1.$2/g')"
-TAG_NAME="dev"
-cd "$REPO_DIR"
+REQUIRED_PLATFORMS=('linux/arm64','linux/amd64','linux/arm/v8','linux/arm/v7')
+
+function check_platforms() {
+    INSTALLED_PLATFORMS="$(docker buildx inspect | grep 'Platforms:' )"
+
+    for REQUIRED_PLATFORM in ${REQUIRED_PLATFORMS//,/$IFS}; do
+        echo "[+] Checking for: $REQUIRED_PLATFORM..."
+        if ! (echo "$INSTALLED_PLATFORMS" | grep -q "$REQUIRED_PLATFORM"); then
+            return 1
+        fi
+    done
+    echo
+    return 0
+}
+
+function create_builder() {
+    echo "[+] Creating new xbuilder for: $REQUIRED_PLATFORMS"
+    echo
+
+    # Switch to buildx builder if already present / previously created
+    docker buildx create --name xbuilder --driver docker-container --bootstrap --use --platform "$REQUIRED_PLATFORMS" || true
+    docker buildx inspect --bootstrap || true
+
+    echo
+}
+
+function recreate_builder() {
+    # Install QEMU binaries for cross-platform building if not installed
+    docker run --privileged --rm 'tonistiigi/binfmt' --install all
 
 
-which docker > /dev/null
+    # remove existing xbuilder
+    docker buildx stop xbuilder || true
+    docker buildx rm xbuilder || true
 
 
+    # Create Docker builder for cross-platform building
+    docker buildx use xbuilder && return 0
 
 
-# Install QEMU binaries for cross-platform building
-docker run --privileged --rm tonistiigi/binfmt --install all || true
+    create_builder
+}
 
 
-# Create Docker builder for cross-platform building
-docker buildx use xbuilder || docker buildx create --name xbuilder --driver docker-container --bootstrap --use
 
 
-# Verify that amd64 and arm64 support are all present
-docker buildx inspect | grep 'amd64.*arm64' || exit 1
+# Check if docker is ready for cross-plaform builds, if not, recreate builder
+docker buildx use xbuilder || create_builder
+check_platforms || (recreate_builder && check_platforms) || exit 1
 
 
 
 
 echo "[+] Building archivebox:$VERSION docker image..."
 echo "[+] Building archivebox:$VERSION docker image..."
 #docker build . \
 #docker build . \
-docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --push . \
+docker buildx build --platform "$REQUIRED_PLATFORMS" --push . \
                -t archivebox \
                -t archivebox \
                -t archivebox:$TAG_NAME \
                -t archivebox:$TAG_NAME \
                -t archivebox:$VERSION \
                -t archivebox:$VERSION \