Skip to content

Commit e2f5409

Browse files
Add prefetch assets GitHub Action
Will fetch the assets once. All subsequent parallel jobs will be able to use them without hitting the origin server.
1 parent df7a8ad commit e2f5409

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Prefetch assets
2+
inputs:
3+
repo-name:
4+
required: true
5+
secrets:
6+
required: true # should be converted with toJSON()
7+
outputs:
8+
cache-key:
9+
value:
10+
runs:
11+
using: "composite"
12+
steps:
13+
- name: Hash asset list
14+
id: hash-asset-list
15+
env:
16+
hash: hashFiles(format('{0}/ci/assets.json', inputs.repo-name))
17+
run: |
18+
"key=$(Get-Date -Format FileDate)-$env:hash" | Out-File $env:GITHUB_OUTPUT -Append
19+
20+
- name: Check if cached
21+
id: check-cache
22+
uses: actions/cache/restore@v4
23+
with:
24+
key: ${{ steps.hash-asset-list.outputs.key }}
25+
path: ${{ github.workspace }}/common/assets
26+
lookup-only: true
27+
28+
- name: Prefetch assets
29+
id: prefetch-assets
30+
if: ${{ !steps.check-cache.outputs.cache-hit }}
31+
env:
32+
repo_name: ${{ inputs.repo-name }}
33+
secrets: ${{ inputs.secrets }}
34+
run: |
35+
foreach ($kv in ($env:secrets | ConvertFrom-Json).GetEnumerator()) { $PSDefaultParameterValues["*.ps1:$($kv.Key)"] = $kv.Value }
36+
& "$env:repo_name/ci/fetch-assets.ps1"
37+
38+
- name: Cache assets
39+
if: steps.prefetch-assets.conclusion == 'success'
40+
uses: actions/cache/save@v4
41+
with:
42+
key: ${{ needs.hash_asset_list.outputs.key }}
43+
path: ${{ github.workspace }}/common/assets

.github/workflows/nightly-publish.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
Configure:
8484
runs-on: ubuntu-latest
8585
outputs:
86-
asset_cache_key: ${{ steps.hash_asset_cache_key.outputs.hash }}
86+
asset_cache_key: ${{ steps.prefetch-assets.outputs.cache-key }}
8787
options: ${{ steps.configure.outputs.options }}
8888
runperformance: ${{ steps.configure.outputs.runperformance }}
8989
version: ${{ steps.configure.outputs.version }}
@@ -96,16 +96,6 @@ jobs:
9696
path: common
9797
ref: ${{ inputs.common-ci-ref }}
9898

99-
- name: Hash the Asset Cache Key
100-
id: hash_asset_cache_key
101-
if: inputs.cache-assets
102-
env:
103-
KEY: ${{ toJSON(secrets) }}
104-
run: |
105-
$sha1 = [Security.Cryptography.SHA1Managed]::new()
106-
$hash = [Convert]::ToHexString($sha1.ComputeHash([Text.Encoding]::Utf8.GetBytes($env:KEY)))
107-
"hash=$(Get-Date -Format FileDate)-$hash" | Out-File $env:GITHUB_OUTPUT -Append
108-
10999
- name: Install GitVersion
110100
run: |
111101
if (Get-Command dotnet-gitversion -ErrorAction SilentlyContinue) {
@@ -128,6 +118,14 @@ jobs:
128118
-GitHubEmail ${{ inputs.email }} `
129119
-BuildPlatform ${{ inputs.build-platform }}
130120
121+
- name: Prefetch assets
122+
id: prefetch-assets
123+
if: hashFiles(format('{0}/ci/assets.json', inputs.repo-name))
124+
uses: ./common/.github/actions/install_runtime
125+
with:
126+
repo-name: ${{ inputs.repo-name }}
127+
secrets: ${{ toJSON(secrets) }}
128+
131129
PreBuild:
132130
needs: Configure
133131
strategy:
@@ -281,13 +279,6 @@ jobs:
281279
-GitHubEmail ${{ inputs.email }} `
282280
-DryRun $DryRun
283281
284-
- name: Save Asset Cache
285-
if: inputs.cache-assets && always()
286-
uses: actions/cache/save@v4
287-
with:
288-
key: ${{ needs.configure.outputs.asset_cache_key }}
289-
path: ${{ github.workspace }}/common/assets
290-
291282
- name: Upload Performance Results Artifact
292283
uses: actions/upload-artifact@v4
293284
if: ${{ matrix.options.RunPerformance }}

.github/workflows/nightly-pull-request.yml

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
runs-on: ubuntu-latest
8080
outputs:
8181
options: ${{ steps.configure.outputs.options }}
82-
asset_cache_key: ${{ steps.hash_asset_cache_key.outputs.hash }}
82+
asset_cache_key: ${{ steps.prefetch-assets.outputs.cache-key }}
8383
steps:
8484
- name: Checkout Common
8585
uses: actions/checkout@v4
@@ -88,16 +88,6 @@ jobs:
8888
path: common
8989
ref: ${{ inputs.common-ci-ref }}
9090

91-
- name: Hash the Asset Cache Key
92-
id: hash_asset_cache_key
93-
if: inputs.cache-assets
94-
env:
95-
KEY: ${{ toJSON(secrets) }}
96-
run: |
97-
$sha1 = [Security.Cryptography.SHA1Managed]::new()
98-
$hash = [Convert]::ToHexString($sha1.ComputeHash([Text.Encoding]::Utf8.GetBytes($env:KEY)))
99-
"hash=$(Get-Date -Format FileDate)-$hash" | Out-File $env:GITHUB_OUTPUT -Append
100-
10191
- name: Configure
10292
id: configure
10393
run: |
@@ -111,6 +101,14 @@ jobs:
111101
-GitHubUser ${{ inputs.user }} `
112102
-GitHubEmail ${{ inputs.email }}
113103
104+
- name: Prefetch assets
105+
id: prefetch-assets
106+
if: hashFiles(format('{0}/ci/assets.json', inputs.repo-name))
107+
uses: ./common/.github/actions/install_runtime
108+
with:
109+
repo-name: ${{ inputs.repo-name }}
110+
secrets: ${{ toJSON(secrets) }}
111+
114112
BuildAndTest:
115113
needs: Configure
116114
strategy:
@@ -135,7 +133,7 @@ jobs:
135133
version: ${{ matrix.options.LanguageVersion }}
136134

137135
- name: Restore Asset Cache
138-
if: inputs.cache-assets
136+
if: needs.configure.outputs.asset_cache_key
139137
uses: actions/cache/restore@v4
140138
with:
141139
key: ${{ needs.configure.outputs.asset_cache_key }}
@@ -161,13 +159,6 @@ jobs:
161159
-GitHubEmail ${{ inputs.email }} `
162160
-DryRun $DryRun
163161
164-
- name: Save Asset Cache
165-
if: inputs.cache-assets && always()
166-
uses: actions/cache/save@v4
167-
with:
168-
key: ${{ needs.configure.outputs.asset_cache_key }}
169-
path: ${{ github.workspace }}/common/assets
170-
171162
- name: Publish Test Results
172163
if: always()
173164
uses: ./common/.github/actions/publish_test_results

steps/fetch-assets.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ $ErrorActionPreference = "Stop"
88

99
$cache = New-Item -ItemType Directory -Path assets -Force
1010

11+
# If assets is a path to assets.json - get the list from it
12+
if ($Assets.Count -eq 1 -and $Assets[0] -like '*.json' -and (Test-Path $Assets[0])) {
13+
$Assets = Get-Content $Assets[0] | ConvertFrom-Json
14+
}
15+
1116
foreach ($asset in $Assets) {
1217
if (Test-Path $cache/$asset) {
1318
Write-Host "Asset '$asset' already present in cache, skipping download"

0 commit comments

Comments
 (0)