Skip to content

Commit 20927af

Browse files
authored
ci(vcpkg): Stabilize vcpkg binary caching; add compiler-aware keys and explicit cache path (#2028)
1 parent 2b37732 commit 20927af

File tree

1 file changed

+69
-17
lines changed

1 file changed

+69
-17
lines changed

.github/workflows/build-toolchain.yml

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,16 @@ jobs:
3131
name: ${{ inputs.preset }}${{ inputs.tools && '+t' || '' }}${{ inputs.extras && '+e' || '' }}
3232
runs-on: windows-2022
3333
timeout-minutes: 30
34+
3435
env:
3536
VCPKG_FILE_CACHE: ${{ github.workspace }}\vcpkg-bincache
3637
VCPKG_BINARY_SOURCES: clear;files,${{ github.workspace }}\vcpkg-bincache,readwrite
3738
VCPKG_FEATURE_FLAGS: manifests,versions,binarycaching
39+
3840
steps:
3941
- name: Checkout Code
4042
uses: actions/checkout@v4
4143

42-
- name: Cache vcpkg binary artifacts
43-
uses: actions/cache@v4
44-
with:
45-
path: ${{ github.workspace }}\vcpkg-bincache
46-
key: vcpkg-bincache-${{ runner.os }}-${{ hashFiles('vcpkg.json','vcpkg-lock.json') }}-${{ inputs.preset }}
47-
restore-keys: |
48-
vcpkg-bincache-${{ runner.os }}-${{ hashFiles('vcpkg.json','vcpkg-lock.json') }}-
49-
vcpkg-bincache-${{ runner.os }}-
50-
5144
- name: Cache VC6 Installation
5245
if: startsWith(inputs.preset, 'vc6')
5346
id: cache-vc6
@@ -112,15 +105,65 @@ jobs:
112105
with:
113106
arch: x86
114107

108+
- name: Compute vcpkg cache key parts
109+
if: startsWith(inputs.preset, 'win32')
110+
id: vcpkg_key
111+
shell: pwsh
112+
run: |
113+
$baseline = (Get-Content vcpkg.json | ConvertFrom-Json)."builtin-baseline"
114+
115+
$msvc = $env:VCToolsVersion
116+
if (-not $msvc) { $msvc = "unknown" }
117+
118+
# Reduce churn: keep major.minor (e.g. 14.44)
119+
$msvcMajorMinor = ($msvc -split '\.')[0..1] -join '.'
120+
121+
$triplet = "x86-windows"
122+
if ("${{ inputs.preset }}" -like "x64*") { $triplet = "x64-windows" }
123+
124+
"baseline=$baseline" >> $env:GITHUB_OUTPUT
125+
"msvc=$msvcMajorMinor" >> $env:GITHUB_OUTPUT
126+
"triplet=$triplet" >> $env:GITHUB_OUTPUT
127+
128+
Write-Host "vcpkg cache key parts: baseline=$baseline, msvc=$msvcMajorMinor, triplet=$triplet"
129+
130+
- name: Restore vcpkg binary cache
131+
if: startsWith(inputs.preset, 'win32')
132+
id: vcpkg_cache
133+
uses: actions/cache/restore@v4
134+
with:
135+
path: ${{ github.workspace }}\vcpkg-bincache
136+
key: vcpkg-bincache-v2-${{ runner.os }}-msvc${{ steps.vcpkg_key.outputs.msvc }}-baseline${{ steps.vcpkg_key.outputs.baseline }}-${{ steps.vcpkg_key.outputs.triplet }}
137+
restore-keys: |
138+
vcpkg-bincache-v2-${{ runner.os }}-msvc${{ steps.vcpkg_key.outputs.msvc }}-baseline${{ steps.vcpkg_key.outputs.baseline }}-
139+
vcpkg-bincache-v2-${{ runner.os }}-
140+
115141
- name: Setup vcpkg
116142
uses: lukka/run-vcpkg@v11
143+
with:
144+
runVcpkgInstall: false
145+
doNotCache: true
146+
147+
- name: Configure vcpkg to use cached directory
148+
if: startsWith(inputs.preset, 'win32')
149+
shell: pwsh
150+
run: |
151+
$cacheDir = "${{ github.workspace }}\vcpkg-bincache"
152+
New-Item -ItemType Directory -Force -Path $cacheDir | Out-Null
153+
154+
# lukka/run-vcpkg sets its own temp cache dir; override to force our cached dir
155+
$env:VCPKG_DEFAULT_BINARY_CACHE = $cacheDir
156+
$env:VCPKG_BINARY_SOURCES = "clear;files,$cacheDir,readwrite"
157+
158+
"VCPKG_DEFAULT_BINARY_CACHE=$cacheDir" >> $env:GITHUB_ENV
159+
"VCPKG_BINARY_SOURCES=$env:VCPKG_BINARY_SOURCES" >> $env:GITHUB_ENV
117160
118161
- name: Configure ${{ inputs.game }} with CMake Using ${{ inputs.preset }}${{ inputs.tools && '+t' || '' }}${{ inputs.extras && '+e' || '' }} Preset
119162
shell: pwsh
120163
run: |
121164
$buildFlags = @(
122-
"-DRTS_BUILD_ZEROHOUR=${{ inputs.game == 'GeneralsMD' && 'ON' || 'OFF' }}",
123-
"-DRTS_BUILD_GENERALS=${{ inputs.game == 'Generals' && 'ON' || 'OFF' }}"
165+
"-DRTS_BUILD_ZEROHOUR=${{ inputs.game == 'GeneralsMD' && 'ON' || 'OFF' }}",
166+
"-DRTS_BUILD_GENERALS=${{ inputs.game == 'Generals' && 'ON' || 'OFF' }}"
124167
)
125168
126169
$gamePrefix = "${{ inputs.game == 'Generals' && 'GENERALS' || 'ZEROHOUR' }}"
@@ -130,27 +173,36 @@ jobs:
130173
$buildFlags += "-DRTS_BUILD_${gamePrefix}_EXTRAS=${{ inputs.extras && 'ON' || 'OFF' }}"
131174
132175
Write-Host "Build flags: $($buildFlags -join ' | ')"
133-
134176
cmake --preset ${{ inputs.preset }} $buildFlags
135177
136178
- name: Build ${{ inputs.game }} with CMake Using ${{ inputs.preset }}${{ inputs.tools && '+t' || '' }}${{ inputs.extras && '+e' || '' }} Preset
137179
shell: pwsh
138180
run: |
139181
cmake --build --preset ${{ inputs.preset }}
140182
183+
- name: Save vcpkg binary cache
184+
# Only one job should save to avoid "Unable to reserve cache" conflicts.
185+
if: ${{ startsWith(inputs.preset, 'win32') && steps.vcpkg_cache.outputs.cache-hit != 'true' && inputs.game == 'Generals' && inputs.preset == 'win32-vcpkg-debug' }}
186+
uses: actions/cache/save@v4
187+
with:
188+
path: ${{ github.workspace }}\vcpkg-bincache
189+
key: vcpkg-bincache-v2-${{ runner.os }}-msvc${{ steps.vcpkg_key.outputs.msvc }}-baseline${{ steps.vcpkg_key.outputs.baseline }}-${{ steps.vcpkg_key.outputs.triplet }}
190+
141191
- name: Collect ${{ inputs.game }} ${{ inputs.preset }}${{ inputs.tools && '+t' || '' }}${{ inputs.extras && '+e' || '' }} Artifact
142192
shell: pwsh
143193
run: |
144194
$buildDir = "build\${{ inputs.preset }}"
145195
$artifactsDir = New-Item -ItemType Directory -Force -Path "$buildDir\${{ inputs.game }}\artifacts" -Verbose
146196
147-
if ("${{ inputs.preset }}" -like "win32*") {
148-
# For win32 preset, look in config-specific subdirectories
197+
if ("${{ inputs.preset }}" -like "win32*") {
149198
$configToUse = if ("${{ inputs.preset }}" -match "debug") { "Debug" } else { "Release" }
150-
$files = Get-ChildItem -Path "$buildDir\Core\$configToUse","$buildDir\${{ inputs.game }}\$configToUse" -File | Where-Object { $_.Extension -in @(".exe", ".dll", ".pdb") } -Verbose
151-
} else {
152-
$files = Get-ChildItem -Path "$buildDir\Core","$buildDir\${{ inputs.game }}" -File | Where-Object { $_.Extension -in @(".exe", ".dll", ".pdb") } -Verbose
199+
$files = Get-ChildItem -Path "$buildDir\Core\$configToUse","$buildDir\${{ inputs.game }}\$configToUse" -File |
200+
Where-Object { $_.Extension -in @(".exe", ".dll", ".pdb") } -Verbose
201+
} else {
202+
$files = Get-ChildItem -Path "$buildDir\Core","$buildDir\${{ inputs.game }}" -File |
203+
Where-Object { $_.Extension -in @(".exe", ".dll", ".pdb") } -Verbose
153204
}
205+
154206
$files | Move-Item -Destination $artifactsDir -Verbose -Force
155207
156208
- name: Upload ${{ inputs.game }} ${{ inputs.preset }}${{ inputs.tools && '+t' || '' }}${{ inputs.extras && '+e' || '' }} Artifact

0 commit comments

Comments
 (0)