From e9bc87fc5eed612a3260df539c255caa6d795b7e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 01:11:54 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01B82eRUAp3wTZAV7UCEiC4S --- .github/actions/apt/action.yml | 79 ++++++++++++++++++++++++++++++++++ .github/workflows/firmware.yml | 14 +++--- .github/workflows/hardware.yml | 13 +++--- 3 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 .github/actions/apt/action.yml diff --git a/.github/actions/apt/action.yml b/.github/actions/apt/action.yml new file mode 100644 index 0000000..089068d --- /dev/null +++ b/.github/actions/apt/action.yml @@ -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 }} diff --git a/.github/workflows/firmware.yml b/.github/workflows/firmware.yml index aac010e..17d7004 100644 --- a/.github/workflows/firmware.yml +++ b/.github/workflows/firmware.yml @@ -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: | diff --git a/.github/workflows/hardware.yml b/.github/workflows/hardware.yml index 997e366..3a9f4e5 100644 --- a/.github/workflows/hardware.yml +++ b/.github/workflows/hardware.yml @@ -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: |