Commit edd3c8a
authored
feat: Optimize Docker image caching layers (#1139)
## Summary
This PR improves Docker build caching so that CI builds are faster and
more cache-efficient — both in terms of Docker layer caching and package
download caching.
---
## What changed
### `EssentialCSharp.Web/Dockerfile`
- **Metadata-first COPY pattern**: copies `.slnx`, `.csproj`,
`Directory.*.props`, `global.json`, and `nuget.config` before `dotnet
restore` — restore layer is only invalidated when dependencies change,
not on every source edit
- **Split restore / build / publish**: finer cache granularity; `COPY .
.` no longer busts the restore layer
- **BuildKit cache mounts**: `--mount=type=cache` for both NuGet
packages (`/root/.nuget/packages`) and npm (`/root/.npm`) — skips
re-downloading packages when lockfiles haven't changed
- **Narrowed frontend COPY inputs**: only `vite.config.js`, `src/`, and
`wwwroot/js/` are copied into the frontend stage — backend-only changes
no longer invalidate the npm build layer
### `.github/workflows/PR-Build-And-Test.yml` &
`Build-Test-And-Deploy.yml`
- **Explicit GHA cache scopes**: `essentialcsharpweb-main`,
`essentialcsharpweb-pr`, `essentialcsharpweb-deploy` — prevents
cross-workflow cache contention
- **`reproducible-containers/buildkit-cache-dance`**: bridges BuildKit's
daemon-local cache mounts to GitHub Actions cache. Without this,
`--mount=type=cache` entries are empty on every CI run (fresh ephemeral
runner = fresh BuildKit daemon). With it:
- `actions/cache` restores host-side cached data for npm and NuGet
- `buildkit-cache-dance` injects that data into BuildKit before the
build
- After the build, updated cache data is extracted back out and saved
**PR vs main branch policy:**
- PR workflow → `skip-extraction: true` (reads cache, doesn't write
back; avoids polluting canonical cache with feature branches)
- Deploy workflow → `skip-extraction: false` (reads and writes; keeps
the canonical cache warm)
---
## Caching architecture (after this PR)
```
actions/cache ←→ buildkit-cache-dance ←→ BuildKit cache mounts (/root/.nuget, /root/.npm)
↕
type=gha layer cache (Docker layer snapshots)
```
Both mechanisms now work end-to-end in CI, not just locally.1 parent 57bcd60 commit edd3c8a
4 files changed
Lines changed: 61 additions & 10 deletions
File tree
- .github/workflows
- EssentialCSharp.Web
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
65 | 82 | | |
66 | 83 | | |
67 | 84 | | |
| |||
74 | 91 | | |
75 | 92 | | |
76 | 93 | | |
77 | | - | |
78 | | - | |
| 94 | + | |
| 95 | + | |
79 | 96 | | |
80 | 97 | | |
81 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
85 | 102 | | |
86 | 103 | | |
87 | 104 | | |
88 | 105 | | |
89 | 106 | | |
90 | 107 | | |
91 | 108 | | |
92 | | - | |
93 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
94 | 113 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
13 | 16 | | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | | - | |
20 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
21 | 29 | | |
| 30 | + | |
22 | 31 | | |
23 | 32 | | |
24 | 33 | | |
| |||
28 | 37 | | |
29 | 38 | | |
30 | 39 | | |
31 | | - | |
32 | | - | |
33 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
34 | 47 | | |
35 | 48 | | |
36 | 49 | | |
| |||
0 commit comments