Skip to content

Install CI's apt packages from the Actions cache - #282

Merged
kahrendt merged 8 commits into
mainfrom
ci-cache-apt-packages
Nov 20, 2026
Merged

kahrendt merged 8 commits into
mainfrom
ci-cache-apt-packages

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Five CI jobs ran sudo apt-get update && sudo apt-get install against Ubuntu's mirror on every run. Over the last 30 CI runs, 145 install steps had a median of 10 s, a p90 of 18 s and a max of 241 s, and 4 took over a minute. The mirror also regularly stalls: a sister repo saw 424 s and 676 s installs and an 11+ minute hang, and an outage blocked CI for half a day. This PR installs the packages from the GitHub Actions cache, so the mirror only matters on a miss, and puts a time limit on the mirror when it is used.

Change

  • New composite action, .github/actions/apt-packages. It installs ccache and libavahi-compat-libdnssd-dev from .deb files in the Actions cache, the same design as Cache CI's apt packages instead of downloading them in every job esphome-libs/micro-flac#68.
    • Cache key: package-list hash, ImageOS, arch and ImageVersion, ending in a delimiter because the cache prefix-matches even the primary key. A restore-key prefix without ImageVersion lets a job use the previous image's entry until a new one is seeded. A stalled cache download gives up after 2 minutes, not 10.
    • On a hit: apt-get install --no-download <packages>, with the cache folder as apt's archive directory. apt uses a cached deb only where its name and size match the version the image's lists pick, so an incomplete entry or one holding other versions falls back to downloading, with a warning. A job that doesn't save keeps the restored debs, so the fallback fetches only what changed. A failed cached install repairs dpkg first, in case the time limit killed it mid-install.
    • On a miss: download with the image's own lists first, running apt-get update only if that fails. The mirror calls share one 300 s budget, and the install is limited to 120 s.
  • New workflow, apt-cache.yml. It seeds the shared entry on push to main, daily (ImageVersion changes with GitHub's image rollouts) and on demand. A cache saved on a PR branch is visible to that PR alone, so PR jobs only restore.
    • A seed only saves debs it downloaded against the image's own lists.
    • It fails if its existing entry no longer installs, because an entry can't be overwritten; bump cache_version to replace it.
    • It checks its entry exists after saving, since actions/cache/save only warns when an upload fails.
  • Lint job: the apt step is gone. It runs the image's preinstalled clang-tidy-21 through a new CLANG_TIDY override in script/clang-tidy.sh. That is the version Ubuntu 26.04's clang-tidy package installed, so findings don't change. The job never needed avahi, because clang-tidy only analyzes src/.
  • Runners: every job is pinned to ubuntu-26.04, which ubuntu-latest already resolves to, so an image change is now deliberate.
  • Dependabot also watches .github/actions/*.

This is not a security boundary

apt checks a cached deb's name and size, not its hash (pkgAcqArchive in apt's acquire-item.cc). So a same-size deb with a planted postinst installs as root. Only someone who can write the cache entry could plant one, and actions/cache extracts with tar -P, so they could write files anywhere without a deb. The exposure is who can write main's cache scope, which every PR restores: any job in a run on main, including the ccache saves in ci.yml and the third-party code those jobs build. Narrowing that is separate work. Same conclusion as esphome-libs/micro-flac#70.

Why not awalsh128/cache-apt-pkgs-action

ESPHome and Home Assistant wrap it (v1.6.3 source read for this):

  • On a miss it installs apt-fast by running quick-install.sh from ilikenwf/apt-fast's master branch. That script is unpinned, and apt-fast isn't on the 24.04 or 26.04 images.
  • Its key has no restore fallback, so every key change downloads from the mirror.
  • It restores files without dpkg or install scripts, which is why ESPHome needs an apt-fast shim against empty caches and HA needs an ldconfig fix.

Its restore is a few seconds faster. Here the install step, including the cache download, takes 8-18 s.

Tests

  • Ubuntu 26.04 container:
    • a cache-miss install, then a restore with networking disabled (2.6 s, both packages registered with dpkg, ccache runs, a dns_sd program links)
    • an entry missing ccache, or holding a ccache deb of a different size, falls back and downloads only that 582 kB deb; an offline entry missing a dependency fails cleanly
    • a same-size tampered ccache deb installs and its postinst runs: expected, see below
    • a blackholed mirror fails the step in 88 s
    • a miss that needed apt-get update is not saved
    • a seed whose entry no longer installs fails
  • An independent review agent found two real issues and one smaller one, all fixed:
    • cached debs were passed to apt as files, which installs them whatever their version
    • the seed re-saved the previous image's debs
    • the image's own apt retry settings were being overridden
  • On GitHub, with a temporary branch trigger and its revert (squash-merge drops both):
    • Seed run 44772635297 saved a 2.7 MB entry. The mirror was slow during it: 2.7 MB took 1 min 48 s, which is why the mirror now gets a 300 s budget.
    • CI run 44772635171 raced the seed and downloaded.
    • Its rerun (attempt 2) installed all four apt jobs from the cache, with zero mirror downloads and no warnings. Three of those jobs ran on the newer image 20261116.202.1 and installed through the prefix fallback.
    • After the key delimiter, restore timeout, shared budget and save check (ported from micro-flac#68's Copilot review), seed run 44778548555 downloaded afresh, saved the delimited key, and its lookup found the entry. CI run 44778549053's rerun then restored all four apt jobs with zero mirror downloads, in 7-17 s.

Until the first push to main seeds the shared entry, PR runs download as before, but with time limits.

The host jobs installed ccache and libavahi-compat-libdnssd-dev from
Ubuntu's mirror on every run, so a stalled mirror stalled or failed CI.
A composite action now installs them from .deb files kept in the
Actions cache, by package name so apt checks each file against the
image's signed lists, and falls back to a time-limited download. A new
workflow seeds the shared entry on main, daily and on demand.

Every job pins ubuntu-26.04, and the lint job uses the image's own
clang-tidy-21 (the version Ubuntu's clang-tidy package installs)
through a new CLANG_TIDY override instead of installing it.
The seed run fetched the 2.7 MB of debs at 24.7 kB/s, which took
1 min 48 s of the 120 s limit.
- The cache key ends in a delimiter: the cache prefix-matches even the
  primary key, so image version .202.1 would also match .202.10.
- A stalled cache download gives up after 2 minutes, not 10.
- The mirror calls share one 300 s budget instead of a limit each.
- The seed checks its entry exists after saving, since actions/cache/save
  only warns when an upload fails.
@kahrendt kahrendt added the github_actions Pull requests that update GitHub Actions code label Nov 20, 2026
apt matches a cached deb by name and size only, not by hash, so the
comments no longer claim a tampered entry is rejected. A job that
doesn't save now keeps the debs it restored when it falls back, so the
download fetches only what changed, and a failed cached install repairs
dpkg first in case the time limit killed it mid-install.
@kahrendt
kahrendt marked this pull request as ready for review November 20, 2026 19:29
@kahrendt
kahrendt requested an adaptive review from Copilot November 20, 2026 19:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cache-seeding runs can remain green when stale package lists prevent cache creation or an existing immutable entry cannot be fully restored, leaving CI repeatedly downloading from mirrors.

2 open issues
What changed in this PR

Moves CI’s apt dependencies into a shared GitHub Actions cache to reduce mirror downloads and limit delays when downloads are necessary. These changes affect CI tooling, not library behavior.

Changes:

  • Adds a cached apt installer and a workflow that seeds the cache on main, daily, and manually.
  • Migrates four build/test jobs to the installer and pins CI runners to Ubuntu 26.04.
  • Uses preinstalled clang-tidy through an executable override and extends Dependabot coverage to composite actions.
File Description
script/​clang-tidy.sh Adds and validates the CLANG_TIDY executable override.
.github/​workflows/​ci.yml Uses cached apt packages, preinstalled clang-tidy, and pinned runners.
.github/​workflows/​apt-cache.yml Adds shared apt-cache seeding and validation runs.
.github/​dependabot.yml Includes composite-action dependencies in updates.
.github/​actions/​apt-packages/​action.yml Implements cache restoration, bounded apt fallback, and cache-save checks.

Review effort: Max (adaptive)

Comment thread .github/actions/apt-packages/action.yml Resolved
Comment thread .github/actions/apt-packages/action.yml Resolved
@kahrendt
kahrendt merged commit 86c7b1c into main Nov 20, 2026
17 checks passed
@kahrendt
kahrendt deleted the ci-cache-apt-packages branch November 20, 2026 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

github_actions Pull requests that update GitHub Actions code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants