CI: cache apt and Homebrew downloads

Package installs sometimes stall on GitHub's runners. A local action
(.github/actions/apt, and .github/actions/brew where macOS builds) caches
the downloaded packages, and apt's package lists with them, keyed on the
runner image and the package list. A cache hit installs offline; a miss
installs as before, with retries and timeouts, and saves the cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRUAp3wTZAV7UCEiC4S
This commit is contained in:
Claude committed 2026-11-01 01:11:54 +00:00
1 parent 2ddc0111f3
commit e9bc87fc5e
3 files changed
+95 -11

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
# apt-get install with its downloads cached (actions/cache): the .debs and
# the package lists they were resolved from, kept together, keyed on the
# runner image (ImageOS, ImageVersion: a new image every week or so) and
# the package list. On a hit the install is offline (--no-download): no
# mirror, nothing to stall on. On a miss, or if the offline install
# can't be done, the usual update and install, with retries and timeouts
# so a stalled mirror fails over instead of hanging; then the cache is
# saved for the next run.
name: apt packages
description: apt-get install, cached
inputs:
packages:
description: the packages (spaces or newlines between)
required: true
ppa:
description: a PPA to add first (ppa:owner/name), or empty
default: ''
recommends:
description: "'true' installs recommended packages too"
default: 'false'
runs:
using: composite
steps:
- id: key
shell: bash
env:
PKGS: ${{ inputs.packages }}
PPA: ${{ inputs.ppa }}
REC: ${{ inputs.recommends }}
run: |
pkgs=$(printf '%s\n' $PKGS | sort -u | tr '\n' ' ')
echo "pkgs=$pkgs" >> "$GITHUB_OUTPUT"
echo "hash=$(printf '%s|%s|%s' "$pkgs" "$PPA" "$REC" | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"
- id: restore
uses: actions/cache/restore@v4
with:
path: /tmp/apt-cache
key: apt-${{ env.ImageOS }}-${{ runner.arch }}-${{ env.ImageVersion }}-${{ steps.key.outputs.hash }}
- id: install
shell: bash
env:
PKGS: ${{ steps.key.outputs.pkgs }}
PPA: ${{ inputs.ppa }}
REC: ${{ inputs.recommends }}
HIT: ${{ steps.restore.outputs.cache-hit }}
run: |
set -euo pipefail
C=/tmp/apt-cache
sudo mkdir -p "$C/archives/partial" "$C/lists/partial"
rec=--no-install-recommends
[ "$REC" = true ] && rec=
apt=(sudo DEBIAN_FRONTEND=noninteractive apt-get -y -q $rec
-o Dir::Cache::Archives="$C/archives" -o Dir::State::Lists="$C/lists"
-o APT::Keep-Downloaded-Packages=true
-o Acquire::Retries=5
-o Acquire::http::Timeout=30 -o Acquire::https::Timeout=30)
if [ -n "$PPA" ]; then
for i in 1 2 3; do sudo add-apt-repository -y -n "$PPA" && break; sleep $((i * 10)); done
fi
fresh=false
if [ "$HIT" = true ] && "${apt[@]}" install --no-download $PKGS; then
echo "installed from the cache"
else
for i in 1 2 3; do "${apt[@]}" update && break; sleep $((i * 10)); done
for i in 1 2 3; do "${apt[@]}" install $PKGS && break; [ $i = 3 ] && exit 1; sleep $((i * 10)); done
fresh=true
fi
sudo rm -rf "$C/archives/partial" "$C/lists/partial" "$C/archives/lock" "$C/lists/lock"
sudo chown -R "$(id -u):$(id -g)" "$C"
echo "fresh=$fresh" >> "$GITHUB_OUTPUT"
- if: steps.install.outputs.fresh == 'true'
uses: actions/cache/save@v4
continue-on-error: true
with:
path: /tmp/apt-cache
key: ${{ steps.restore.outputs.cache-primary-key }}
+9 -5
View File
@@ -4,9 +4,9 @@ name: firmware
on:
push:
paths: ["firmware/**", "hardware/netlist/**", ".github/workflows/firmware.yml"]
paths: ["firmware/**", "hardware/netlist/**", ".github/workflows/firmware.yml", ".github/actions/apt/**"]
pull_request:
paths: ["firmware/**", "hardware/netlist/**", ".github/workflows/firmware.yml"]
paths: ["firmware/**", "hardware/netlist/**", ".github/workflows/firmware.yml", ".github/actions/apt/**"]
jobs:
build-and-test:
@@ -14,10 +14,14 @@ jobs:
steps:
- uses: actions/checkout@v4
# (apt's downloads cached: .github/actions/apt)
- name: Tools
run: |
sudo apt-get update -qq
sudo apt-get install -y -qq gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib ninja-build cmake
uses: ./.github/actions/apt
with:
packages: >-
gcc-arm-none-eabi libnewlib-arm-none-eabi
libstdc++-arm-none-eabi-newlib ninja-build cmake
recommends: 'true'
- name: Pin header matches the netlist
run: |
+7 -6
View File
@@ -9,9 +9,9 @@ name: hardware
on:
push:
paths: ["hardware/**", ".github/workflows/hardware.yml"]
paths: ["hardware/**", ".github/workflows/hardware.yml", ".github/actions/apt/**"]
pull_request:
paths: ["hardware/**", ".github/workflows/hardware.yml"]
paths: ["hardware/**", ".github/workflows/hardware.yml", ".github/actions/apt/**"]
jobs:
check:
@@ -19,11 +19,12 @@ jobs:
steps:
- uses: actions/checkout@v4
# (apt's downloads cached: .github/actions/apt)
- name: KiCad 9
run: |
sudo add-apt-repository -y ppa:kicad/kicad-9.0-releases
sudo apt-get update -qq
sudo apt-get install -y -qq --no-install-recommends kicad
uses: ./.github/actions/apt
with:
ppa: ppa:kicad/kicad-9.0-releases
packages: kicad
- name: Netlists and schematics match the netlist script
run: |