From db3c98fd6e3ab3020427e55b55108904f8a5f9a7 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 01:11:52 +0000 Subject: [PATCH 1/2] 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/actions/brew/action.yml | 49 ++++++++++++++++++++ .github/workflows/android.yml | 23 +++++++--- .github/workflows/apple.yml | 8 ++-- 4 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 .github/actions/apt/action.yml create mode 100644 .github/actions/brew/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/actions/brew/action.yml b/.github/actions/brew/action.yml new file mode 100644 index 0000000..7b8af9e --- /dev/null +++ b/.github/actions/brew/action.yml @@ -0,0 +1,49 @@ +# brew install with Homebrew's downloads cached (actions/cache): the +# bottles and its formula index (the API files), keyed on the runner image +# (ImageOS, ImageVersion) and the package list. No brew update (the slow, +# networked part; the image's Homebrew is recent enough), and with the +# index and bottles cached a hit downloads nothing. Retried, in case a +# download stalls. +name: brew packages +description: brew install, cached +inputs: + packages: + description: the formulae (spaces or newlines between) + required: true +runs: + using: composite + steps: + - id: key + shell: bash + env: + PKGS: ${{ inputs.packages }} + run: | + pkgs=$(printf '%s\n' $PKGS | sort -u | tr '\n' ' ') + echo "pkgs=$pkgs" >> "$GITHUB_OUTPUT" + echo "hash=$(printf '%s' "$pkgs" | shasum -a 256 | cut -c1-16)" >> "$GITHUB_OUTPUT" + + - id: restore + uses: actions/cache/restore@v4 + with: + path: /tmp/brew-cache + key: brew-${{ env.ImageOS }}-${{ runner.arch }}-${{ env.ImageVersion }}-${{ steps.key.outputs.hash }} + + - shell: bash + env: + PKGS: ${{ steps.key.outputs.pkgs }} + HOMEBREW_CACHE: /tmp/brew-cache + HOMEBREW_NO_AUTO_UPDATE: '1' + HOMEBREW_NO_INSTALL_CLEANUP: '1' + HOMEBREW_NO_ENV_HINTS: '1' + HOMEBREW_CURL_RETRIES: '5' + run: | + set -euo pipefail + for i in 1 2 3; do brew install $PKGS && exit 0; sleep $((i * 10)); done + exit 1 + + - if: steps.restore.outputs.cache-hit != 'true' + uses: actions/cache/save@v4 + continue-on-error: true + with: + path: /tmp/brew-cache + key: ${{ steps.restore.outputs.cache-primary-key }} diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index cfe4551..3a4ce9e 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -8,6 +8,9 @@ # release: the release APK and bundle (R8): signed with the release # environment's key for main and v* tags, the debug key for a pull request. # +# Packages: apt's downloads are cached (.github/actions/apt), keyed on +# the runner image and the package list. +# # D-Modem-RE is a private submodule: the DMODEM_TOKEN secret is a token # that can read it (a fine-grained personal access token with Contents: # read on D-Modem/D-Modem). @@ -76,7 +79,11 @@ jobs: "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" - name: OpenSSH, PuTTY, mksh (the interop tests), gawk (proot's build) - run: sudo apt-get install -y -q openssh-client openssh-server putty-tools gawk mksh && sudo mkdir -p /run/sshd + uses: ./.github/actions/apt + with: + packages: openssh-client openssh-server putty-tools gawk mksh + recommends: 'true' + - run: sudo mkdir -p /run/sshd # the keystore from its secret, for the build (app/build.gradle.kts); none on a fork's pull request - name: Signing keystore env: @@ -146,7 +153,9 @@ jobs: "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" - name: gawk (proot's build) - run: sudo apt-get install -y -q gawk + uses: ./.github/actions/apt + with: + packages: gawk # the keystore from its secret, for the build (app/build.gradle.kts); none on a fork's pull request - name: Signing keystore @@ -192,9 +201,9 @@ jobs: token: ${{ secrets.DMODEM_TOKEN || github.token }} - name: Packages - run: | - sudo apt-get update - sudo apt-get install -y --no-install-recommends cmake ninja-build g++ libssl-dev libgtest-dev + uses: ./.github/actions/apt + with: + packages: cmake ninja-build g++ libssl-dev libgtest-dev - name: Build run: | @@ -240,7 +249,9 @@ jobs: - uses: gradle/actions/setup-gradle@v4 - name: gawk (proot's build) - run: sudo apt-get install -y -q gawk + uses: ./.github/actions/apt + with: + packages: gawk - name: KVM run: | diff --git a/.github/workflows/apple.yml b/.github/workflows/apple.yml index fd1571c..ffe3262 100644 --- a/.github/workflows/apple.yml +++ b/.github/workflows/apple.yml @@ -31,8 +31,11 @@ jobs: submodules: recursive token: ${{ secrets.DMODEM_TOKEN || github.token }} - - name: Ninja - run: brew install ninja + # (Homebrew's downloads cached: .github/actions/brew) + - name: Ninja, XcodeGen + uses: ./.github/actions/brew + with: + packages: ninja xcodegen - uses: actions/setup-java@v4 with: @@ -62,7 +65,6 @@ jobs: # arm64 only: the shared Kotlin's XCFramework has no x86_64 simulator slice - name: The iOS app (simulator) run: | - brew install xcodegen cd ios/DModem && xcodegen generate xcodebuild -project DModem.xcodeproj -scheme DModem \ -destination 'generic/platform=iOS Simulator' \ From aa3e40e1856836509973a9fe4ab09f9daa1e0665 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 01:25:21 +0000 Subject: [PATCH 2/2] CI: key the package caches on the runner image ImageOS and ImageVersion are the runner's environment variables, not the workflow's env context, so ${{ env.ImageVersion }} was empty and the cache key never changed with the image. Build the key in the shell. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRUAp3wTZAV7UCEiC4S --- .github/actions/apt/action.yml | 5 +++-- .github/actions/brew/action.yml | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/actions/apt/action.yml b/.github/actions/apt/action.yml index 089068d..d6a60f2 100644 --- a/.github/actions/apt/action.yml +++ b/.github/actions/apt/action.yml @@ -30,13 +30,14 @@ runs: 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" + # (ImageOS, ImageVersion: the runner's environment, not the env context) + echo "key=apt-${ImageOS:-none}-$RUNNER_ARCH-${ImageVersion:-none}-$(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 }} + key: ${{ steps.key.outputs.key }} - id: install shell: bash diff --git a/.github/actions/brew/action.yml b/.github/actions/brew/action.yml index 7b8af9e..54d2287 100644 --- a/.github/actions/brew/action.yml +++ b/.github/actions/brew/action.yml @@ -20,13 +20,14 @@ runs: run: | pkgs=$(printf '%s\n' $PKGS | sort -u | tr '\n' ' ') echo "pkgs=$pkgs" >> "$GITHUB_OUTPUT" - echo "hash=$(printf '%s' "$pkgs" | shasum -a 256 | cut -c1-16)" >> "$GITHUB_OUTPUT" + # (ImageOS, ImageVersion: the runner's environment, not the env context) + echo "key=brew-${ImageOS:-none}-$RUNNER_ARCH-${ImageVersion:-none}-$(printf '%s' "$pkgs" | shasum -a 256 | cut -c1-16)" >> "$GITHUB_OUTPUT" - id: restore uses: actions/cache/restore@v4 with: path: /tmp/brew-cache - key: brew-${{ env.ImageOS }}-${{ runner.arch }}-${{ env.ImageVersion }}-${{ steps.key.outputs.hash }} + key: ${{ steps.key.outputs.key }} - shell: bash env: