fix: add retry logic for flaky Raspbian mirror downloads

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ordinarthur 2026-04-13 22:22:04 +02:00
parent 2f6a271098
commit 1b9be90018
2 changed files with 40 additions and 4 deletions

View File

@ -14,7 +14,7 @@ RUN apt-get update && apt-get install -y \
WORKDIR /build
# Clone pi-gen
# Clone pi-gen (bookworm branch for Raspberry Pi OS stable)
RUN git clone --depth 1 --branch bookworm https://github.com/RPi-Distro/pi-gen.git /build/pi-gen
# Copy config and stage
@ -24,10 +24,13 @@ COPY stage-tipote/ /build/pi-gen/stage-tipote/
# Skip stage2 image export (we export from stage-tipote)
RUN touch /build/pi-gen/stage2/SKIP_IMAGES
# Patch pi-gen for Docker Desktop compatibility:
# 1. Remove setarch linux32 calls (fails on Docker Desktop)
# Patch pi-gen for Docker compatibility:
# 1. Remove setarch linux32 calls (fails on some Docker setups)
# 2. Remove qemu-user-binfmt from dependency check (conflicts with qemu-user-static)
RUN find /build/pi-gen -name "*.sh" -exec sed -i 's/setarch linux32 //g' {} + 2>/dev/null || true \
&& sed -i '/qemu-user-binfmt/d' /build/pi-gen/depends
CMD ["bash", "-c", "modprobe binfmt_misc 2>/dev/null || true && mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null || true && cd /build/pi-gen && ./build.sh && mkdir -p /output && cp deploy/*.img.xz /output/ 2>/dev/null || cp deploy/*.img /output/ 2>/dev/null && ls -lh /output/"]
COPY entrypoint.sh /build/entrypoint.sh
RUN chmod +x /build/entrypoint.sh
CMD ["/build/entrypoint.sh"]

View File

@ -0,0 +1,33 @@
#!/bin/bash
set -euo pipefail
# Load binfmt_misc for ARM emulation
modprobe binfmt_misc 2>/dev/null || true
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null || true
cd /build/pi-gen
# Retry build up to 3 times (Raspbian mirrors can be flaky)
MAX_RETRIES=3
for attempt in $(seq 1 $MAX_RETRIES); do
echo "=== Build attempt $attempt/$MAX_RETRIES ==="
if ./build.sh; then
echo "=== Build succeeded on attempt $attempt ==="
mkdir -p /output
cp deploy/*.img.xz /output/ 2>/dev/null || cp deploy/*.img /output/ 2>/dev/null
ls -lh /output/
exit 0
fi
if [ "$attempt" -lt "$MAX_RETRIES" ]; then
echo "=== Build failed, cleaning up for retry... ==="
# Clean debootstrap rootfs so it starts fresh
rm -rf work/*/stage0/rootfs 2>/dev/null || true
rm -rf work/*/stage0/debootstrap* 2>/dev/null || true
sleep 5
fi
done
echo "=== All $MAX_RETRIES attempts failed ==="
exit 1