Commit e0a47d7f authored by Matt Mastracci's avatar Matt Mastracci

Use host APT mirror in Docker builds

Extract the APT mirror URL from the host's /etc/apt/sources.list
(e.g. Hetzner mirror) and pass it as a build arg to the Dockerfile.
Speeds up apt-get on cache miss when building on Hetzner runners.
parent e9df3a17
...@@ -64,6 +64,21 @@ runs: ...@@ -64,6 +64,21 @@ runs:
process.stdout.write(JSON.stringify(c, null, 2)); process.stdout.write(JSON.stringify(c, null, 2));
" > ~/.docker/config.json " > ~/.docker/config.json
- name: Detect host APT mirror
id: apt-mirror
shell: bash
run: |
# Extract the mirror base URL from the host's sources.list.
# Hetzner uses: http://mirror.hetzner.com/ubuntu/packages
# We grab everything before the suite name (jammy, focal, etc).
MIRROR=$(grep -oP 'https?://\S+(?=\s+(focal|jammy|noble)\b)' /etc/apt/sources.list 2>/dev/null | head -1)
if [ -n "$MIRROR" ]; then
echo "url=${MIRROR}" >> "$GITHUB_OUTPUT"
echo "Detected host APT mirror: ${MIRROR}"
else
echo "url=" >> "$GITHUB_OUTPUT"
fi
- name: Build Docker image - name: Build Docker image
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
with: with:
...@@ -72,5 +87,7 @@ runs: ...@@ -72,5 +87,7 @@ runs:
target: ${{ inputs.target || '' }} target: ${{ inputs.target || '' }}
load: true load: true
tags: ${{ inputs.image-tag }} tags: ${{ inputs.image-tag }}
build-args: |
APT_MIRROR=${{ steps.apt-mirror.outputs.url }}
cache-from: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache cache-from: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache
cache-to: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache,mode=max cache-to: type=registry,ref=${{ steps.resolve.outputs.host }}/${{ inputs.cache-ref }}:cache,mode=max
...@@ -20,16 +20,20 @@ FROM ubuntu:20.04 AS builder ...@@ -20,16 +20,20 @@ FROM ubuntu:20.04 AS builder
# Avoid interactive prompts during apt-get # Avoid interactive prompts during apt-get
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
# If the host provides an apt mirror URL (e.g. Hetzner), use it for the
# native architecture. The suite names come from the container's own OS.
ARG APT_MIRROR=
# Enable multiarch for cross-compilation sysroots. # Enable multiarch for cross-compilation sysroots.
# Write sources.list from scratch with explicit [arch=...] tags. # Write sources.list from scratch with explicit [arch=...] tags.
# On arm64 hosts: native packages from ports, foreign amd64 from archive. # On arm64 hosts: native packages from ports, foreign amd64 from archive.
# On amd64 hosts: native packages from archive, foreign arm64 from ports. # On amd64 hosts: native packages from archive, foreign arm64 from ports.
RUN HOST_ARCH=$(dpkg --print-architecture) && \ RUN HOST_ARCH=$(dpkg --print-architecture) && \
if [ "$HOST_ARCH" = "arm64" ]; then \ if [ "$HOST_ARCH" = "arm64" ]; then \
NATIVE_MIRROR="http://ports.ubuntu.com/ubuntu-ports"; FOREIGN_ARCH=amd64; \ NATIVE_MIRROR="${APT_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}"; FOREIGN_ARCH=amd64; \
FOREIGN_MIRROR="http://archive.ubuntu.com/ubuntu"; \ FOREIGN_MIRROR="http://archive.ubuntu.com/ubuntu"; \
else \ else \
NATIVE_MIRROR="http://archive.ubuntu.com/ubuntu"; FOREIGN_ARCH=arm64; \ NATIVE_MIRROR="${APT_MIRROR:-http://archive.ubuntu.com/ubuntu}"; FOREIGN_ARCH=arm64; \
FOREIGN_MIRROR="http://ports.ubuntu.com/ubuntu-ports"; \ FOREIGN_MIRROR="http://ports.ubuntu.com/ubuntu-ports"; \
fi && \ fi && \
dpkg --add-architecture "$FOREIGN_ARCH" && \ dpkg --add-architecture "$FOREIGN_ARCH" && \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment