@@ -15,18 +15,55 @@ jobs:
15
15
- name : Checkout Repository
16
16
uses : actions/checkout@v4
17
17
18
- - name : Download BuildKit
18
+ - name : Restore Cached Image TAR
19
+ uses : actions/cache@v4
20
+ with :
21
+ path : cache-image.tar
22
+ key : docker-image-${{ runner.os }}-${{ github.sha }}
23
+ restore-keys : |
24
+ docker-image-${{ runner.os }}-
25
+
26
+ - name : Load Cached Image
27
+ shell : pwsh
28
+ id : load_cache
19
29
run : |
20
- curl -SL --output buildx.exe https://github.com/docker/buildx/releases/download/v0.22.0-rc1/buildx-v0.22.0-rc1.windows-amd64.exe
30
+ if (Test-Path cache-image.tar) {
31
+ docker load -i cache-image.tar
32
+ echo "CACHE_HIT=true" >> $env:GITHUB_ENV
33
+ }
21
34
22
- - name : Build Image
35
+ - name : Build Image Without Cache
36
+ if : env.CACHE_HIT != 'true'
23
37
shell : pwsh
24
38
run : |
25
39
docker build -f docker/mount.dockerfile -t app:latest .
26
40
27
- - name : Cache Image with GHA
41
+ - name : Build Image With Cache
42
+ if : env.CACHE_HIT == 'true'
43
+ shell : pwsh
44
+ run : |
45
+ New-Item -Path build.log -ItemType File -Force
46
+ docker build --cache-from app:latest -f docker/mount.dockerfile -t app:latest . | Tee-Object -FilePath build.log
47
+ $cache_hits = (Get-Content build.log | Select-String -Pattern '---> Using cache' | Measure-Object).Count
48
+ $steps_with_cache = (Get-Content build.log | Select-String -Pattern '^Step [0-9]+/' | Where-Object { $_ -notmatch 'FROM' } | Measure-Object).Count
49
+ if ($cache_hits -eq $steps_with_cache) {
50
+ echo "FULL_CACHE_HIT=true" >> $env:GITHUB_ENV
51
+ Write-Host "Full cache hit, all layers were used from source cache."
52
+ } else {
53
+ Write-Host "Partial cache hit, some layers were rebuilt."
54
+ }
55
+
56
+ - name : Save Docker Image to TAR
57
+ if : env.FULL_CACHE_HIT != 'true'
28
58
shell : pwsh
29
59
run : |
30
- $env:DOCKER_BUILDKIT=1
31
- $env:BUILDX_EXPERIMENTAL=1
32
- .\buildx.exe build --push -f docker/xcache.dockerfile --cache-to type=gha,mode=max --cache-from type=gha -t docker.io/library/app:latest .
60
+ docker save -o cache-image.tar app:latest
61
+
62
+ - name : Cache Image TAR
63
+ if : env.FULL_CACHE_HIT != 'true'
64
+ uses : actions/cache@v4
65
+ with :
66
+ path : cache-image.tar
67
+ key : docker-image-${{ runner.os }}-${{ github.sha }}
68
+ restore-keys : |
69
+ docker-image-${{ runner.os }}-
0 commit comments