Skip to content

Commit 6add851

Browse files
authored
Merge pull request #1 from AnastaZIuk/master
Cache hits
2 parents b6fa4ca + 734ca08 commit 6add851

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

.github/workflows/main.yml

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,55 @@ jobs:
1515
- name: Checkout Repository
1616
uses: actions/checkout@v4
1717

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
1929
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+
}
2134
22-
- name: Build Image
35+
- name: Build Image Without Cache
36+
if: env.CACHE_HIT != 'true'
2337
shell: pwsh
2438
run: |
2539
docker build -f docker/mount.dockerfile -t app:latest .
2640
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'
2858
shell: pwsh
2959
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

Comments
 (0)