Skip to content

Commit 5bed472

Browse files
committed
Merge branch 'more-cache-reuse'
2 parents 3a1bf8d + f83ae03 commit 5bed472

File tree

5 files changed

+136
-37
lines changed

5 files changed

+136
-37
lines changed

.github/workflows/build-test.yml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ on:
1515
pv:
1616
description: The $PV value from the cygport file
1717
value: ${{ jobs.build-test.outputs.pv }}
18+
cache-found:
19+
description: Whether an existing build cache was found and used
20+
value: ${{ jobs.build-test.outputs.cache-found }}
1821

1922
jobs:
2023
build-test:
@@ -24,6 +27,7 @@ jobs:
2427
CYGPORT_FILE: ${{ inputs.cygport_file }}
2528
outputs:
2629
pv: ${{ steps.data.outputs.pv }}
30+
cache-found: ${{ steps.build-cache.outputs.cache-hit == 'true' }}
2731
steps:
2832

2933
- name: Configure Git for Windows' core.autocrlf
@@ -90,7 +94,17 @@ jobs:
9094
9195
printf 'pv=%s\n' "$PV" | tee -a "$GITHUB_OUTPUT"
9296
97+
# Cache hit here means we can skip most of the rest of this job, since
98+
# this job is essentially building this cache.
99+
- name: Use build cache
100+
id: build-cache
101+
uses: actions/cache@v3
102+
with:
103+
key: ${{ env.CYGPORT_NAME }}-build-${{ github.sha }}
104+
path: build-results.tar
105+
93106
- name: Install Cygwin build requirements
107+
if: steps.build-cache.outputs.cache-hit != 'true'
94108
uses: cygwin/cygwin-install-action@db475590d56881c6cef7b3f96f6f3dd9532ea1f4
95109
with:
96110
packages: ${{ env.CYGPORT_BUILD_REQUIRES }}
@@ -131,64 +145,59 @@ jobs:
131145
path: C:\cygwin\var\log\
132146

133147
- name: Cygport download
148+
if: steps.build-cache.outputs.cache-hit != 'true'
134149
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
135150
env:
136151
PATH: C:\cygwin\bin
137152
run: cygport "$CYGPORT_FILE" download
138153

139154
- name: Cygport prep
155+
if: steps.build-cache.outputs.cache-hit != 'true'
140156
id: cygport-prep
141157
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
142158
env:
143159
PATH: C:\cygwin\bin
144160
run: cygport "$CYGPORT_FILE" prep
145161

146162
- name: Cygport compile
163+
if: steps.build-cache.outputs.cache-hit != 'true'
147164
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
148165
env:
149166
PATH: C:\cygwin\bin
150167
run: cygport "$CYGPORT_FILE" compile
151168

152169
- name: Cygport test
170+
if: steps.build-cache.outputs.cache-hit != 'true'
153171
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
154172
env:
155173
PATH: C:\cygwin\bin
156174
run: cygport "$CYGPORT_FILE" test
157175

158176
- name: Cygport install
177+
if: steps.build-cache.outputs.cache-hit != 'true'
159178
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
160179
env:
161180
PATH: C:\cygwin\bin
162181
run: cygport "$CYGPORT_FILE" install
163182

164183
- name: Cygport package
184+
if: steps.build-cache.outputs.cache-hit != 'true'
165185
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
166186
env:
167187
PATH: C:\cygwin\bin
168188
run: cygport "$CYGPORT_FILE" package
169189

170190
- name: Tar up build results
171-
if: always()
191+
if: always() && steps.cygport-prep.outcome != 'skipped'
172192
shell: C:\cygwin\bin\bash.exe --noprofile --norc -e -o pipefail -o igncr {0}
173193
env:
174194
PATH: C:\cygwin\bin
175195
run: tar -cvf build-results.tar "$CYGPORT_BUILD_DIR"
176196

177197
- name: Store build results
178-
if: always() && steps.cygport-prep.outcome == 'success'
198+
if: always() && steps.cygport-prep.outcome != 'skipped'
179199
uses: actions/upload-artifact@v3
180200
with:
181201
name: build-results
182202
path: build-results.tar
183203
if-no-files-found: error
184-
185-
# Artifacts are great for letting me download the files, and passing
186-
# files between jobs in the same action, but not for passing files
187-
# between actions. Cache the build results so they can be used by a
188-
# separate release action.
189-
- name: Cache build results
190-
if: startsWith(github.ref, 'refs/heads/')
191-
uses: actions/cache/save@v3
192-
with:
193-
key: ${{ env.CYGPORT_NAME }}-build-${{ github.ref_name }}-${{ github.sha }}
194-
path: build-results.tar

.github/workflows/prep-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
id: build-cache
122122
uses: actions/cache/restore@v3
123123
with:
124-
key: ${{ env.CYGPORT_NAME }}-build-${{ github.ref_name }}-${{ github.sha }}
124+
key: ${{ env.CYGPORT_NAME }}-build-${{ github.sha }}
125125
path: build-results.tar
126126

127127
- name: Abort on cache miss

.github/workflows/release.yml

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,11 @@ jobs:
105105
printf 'CYGPORT_PF=%s\n' "$PF" | tee -a "$GITHUB_ENV"
106106
printf 'CYGPORT_ARCH=%s\n' "$ARCH" | tee -a "$GITHUB_ENV"
107107
108-
- name: Get the release target branch
109-
id: target
110-
uses: actions/github-script@v6
111-
env:
112-
TAG_NAME: ${{ inputs.tag_name }}
113-
with:
114-
script: |
115-
const releaseRsp = await github.rest.repos.getReleaseByTag({
116-
owner: context.repo.owner,
117-
repo: context.repo.repo,
118-
tag: process.env.TAG_NAME
119-
});
120-
const targetCommitish = releaseRsp.data.target_commitish;
121-
if (targetCommitish.startsWith('refs/heads/')) {
122-
targetRef = targetCommitish.substring(11);
123-
} else if (target_commitish.startsWith('refs/tags/')) {
124-
targetRef = targetCommitish.substring(10);
125-
} else {
126-
core.setFailed('Unexpected target_commitish: ' + targetCommitish);
127-
}
128-
core.setOutput('target_ref', targetRef);
129-
130108
- name: Get build cache
131109
id: build-cache
132110
uses: actions/cache/restore@v3
133111
with:
134-
key: ${{ env.CYGPORT_NAME }}-build-${{ steps.target.outputs.target_ref }}-${{ github.sha }}
112+
key: ${{ env.CYGPORT_NAME }}-build-${{ github.sha }}
135113
path: build-results.tar
136114

137115
- name: Abort on cache miss

.github/workflows/workflow-tests.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Test workflows
2+
on: [push, pull_request]
3+
4+
# Avoid multiple caching tests stomping over each other
5+
concurrency: mainline-cache-${{ github.sha }}
6+
7+
jobs:
8+
# We need to start with a clean slate for this SHA, so we can distinguish
9+
# between caches created in different ref contexts.
10+
clear-caches:
11+
name: Clear any existing caches
12+
permissions:
13+
actions: write
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Clear existing caches for this commit
17+
uses: actions/github-script@v6
18+
with:
19+
script: |
20+
try {
21+
const response = await github.rest.actions.deleteActionsCacheByKey({
22+
owner: context.repo.owner,
23+
repo: context.repo.repo,
24+
key: `mainline-build-${context.sha}`
25+
});
26+
core.info(`Deleted ${response.data.total_count} caches`);
27+
if (core.isDebug()) {
28+
for (const cache of response.data.actions_caches) {
29+
core.debug(`Deleted cache ID ${cache.id}, ref ${cache.ref}, key ${cache.key}, version ${cache.version}`);
30+
}
31+
}
32+
} catch(e) {
33+
if (e.response.status == 404) {
34+
core.info('No caches to delete');
35+
return;
36+
}
37+
throw(e); // Not the error we were looking for
38+
}
39+
40+
mainline:
41+
name: Basic mainline test
42+
needs: clear-caches
43+
uses: ./.github/workflows/build-test.yml
44+
with:
45+
cygport_file: t/mainline.cygport
46+
47+
ensure-build-cache:
48+
name: Check there's a current build cache
49+
needs: mainline
50+
runs-on: ubuntu-latest
51+
steps:
52+
- name: Check number of caches for the mainline build
53+
id: check
54+
uses: actions/github-script@v6
55+
with:
56+
script: |
57+
const response = await github.rest.actions.getActionsCacheList({
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
key: 'mainline-build-' + context.sha
61+
});
62+
core.info(`Found ${response.data.total_count} caches`);
63+
if (response.data.total_count != 1) {
64+
core.setFailed(`Expected one cache entry, found ${response.data.total_count}.`);
65+
if (response.data.total_count > 0) {
66+
core.startGroup('Cache list');
67+
response.data.actions_caches.forEach(cache =>
68+
core.info(`cache id: ${cache.id}, ref: ${cache.ref}, key: ${cache.key}, version: ${cache.version}`));
69+
core.endGroup();
70+
}
71+
}
72+
73+
mainline-use-cache:
74+
name: Check a build won't create a new cache unnecessarily
75+
needs: mainline
76+
uses: ./.github/workflows/build-test.yml
77+
with:
78+
cygport_file: t/mainline.cygport
79+
80+
ensure-cache-used:
81+
name: Check the build did use the extant cache
82+
needs: mainline-use-cache
83+
runs-on: ubuntu-latest
84+
steps:
85+
- name: Error if cache unused
86+
if: needs.mainline-use-cache.outputs.cache-found != 'true'
87+
run: |
88+
echo '::error title=Cache not used::Cache reportedly not used for rebuild'
89+
exit 1

t/mainline.cygport

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Most basic test cygport file I can manage
2+
NAME=mainline
3+
VERSION=1
4+
RELEASE=1
5+
CATEGORY=Test
6+
SUMMARY='Test package'
7+
HOMEPAGE=
8+
LICENSE=
9+
10+
src_compile () {
11+
cd "$B"
12+
{
13+
echo '#!/usr/bin/env sh'
14+
echo "echo 'Hello, world!'"
15+
} >helloworld
16+
}
17+
18+
src_install () {
19+
cd "$B"
20+
dobin helloworld
21+
}
22+
23+
# vim: set ft=bash noexpandtab tabstop=8 listchars=tab\:\ \ ,trail\:-,lead\:- :

0 commit comments

Comments
 (0)