Skip to content

Commit f5ad0d3

Browse files
committed
internal/ci/base: add helpers for OS + Go version matrices
It's still up to each workflow to set up the matrix and use these, but being able to reuse these common bits is nice. Inline a `let` and a hidden field too, as the added indirection just made the matrix setup harder to follow. While here, cue fmt internal/ci. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ic7d9099928944f6842ad069683e9fbdb6b81a79d Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1219301 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent b95b211 commit f5ad0d3

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

.github/workflows/trybot.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
strategy:
2222
fail-fast: false
2323
matrix:
24-
go-version:
25-
- 1.23.x
26-
- 1.24.x
2724
runner:
2825
- ns-linux-amd64-large
2926
- ns-macos-arm64
3027
- ns-windows-amd64
28+
go-version:
29+
- 1.23.x
30+
- 1.24.x
3131
runs-on: ${{ matrix.runner }}
3232
if: |-
3333
(contains(github.event.head_commit.message, '

internal/ci/base/github.cue

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,27 @@ bashWorkflow: githubactions.#Workflow & {
1919
jobs: [string]: defaults: run: shell: "bash --noprofile --norc -euo pipefail {0}"
2020
}
2121

22+
// These are useful for workflows where we use a matrix over different OS runners
23+
// or multiple Go versions. Note that the matrix names must match.
24+
matrixRunnerName: "runner"
25+
matrixRunnerExpr: "matrix.\(matrixRunnerName)"
26+
matrixGoVersionName: "go-version"
27+
matrixGoVersionExpr: "matrix.\(matrixGoVersionName)"
28+
29+
// isLatestGoLinux is a GitHub expression that evaluates to true if the job
30+
// is running on Linux with the latest version of Go. This expression is often
31+
// used to run certain steps just once per CI workflow, to avoid duplicated work.
32+
isLatestGoLinux: "(\(matrixGoVersionExpr) == '\(latestGo)' && \(matrixRunnerExpr) == '\(linuxMachine)')"
33+
2234
installGo: {
2335
#setupGo: githubactions.#Step & {
2436
name: "Install Go"
2537
uses: "actions/setup-go@v5"
2638
with: {
2739
// We do our own caching in setupCaches.
28-
cache: false
29-
"go-version": string
40+
cache: false
41+
// Allow overriding when using matrixGoVersionExpr.
42+
"go-version": string | *latestGo
3043
}
3144
}
3245

@@ -167,7 +180,7 @@ setupCaches: {
167180
// We skip the cache entirely on the nightly runs, to catch flakes.
168181
// Note that this conditional is just a no-op for jobs without a nightly schedule.
169182
// TODO(mvdan): remove the windowsMachine condition once Windows supports caching on Namespace.
170-
if: "github.event_name != 'schedule' && matrix.runner != '\(windowsMachine)'"
183+
if: "github.event_name != 'schedule' && \(matrixRunnerExpr) != '\(windowsMachine)'"
171184
uses: "namespacelabs/nscloud-cache-action@v1"
172185
with: {
173186
let cacheModes = list.Concat([[
@@ -181,7 +194,7 @@ setupCaches: {
181194
cache: strings.Join(cacheModes, "\n")
182195
}
183196
if len(cachePaths) > 0 {
184-
path: strings.Join(cachePaths, "\n")
197+
path: strings.Join(cachePaths, "\n")
185198
}
186199
}
187200
},
@@ -210,7 +223,7 @@ staticcheck: githubactions.#Step & {
210223
#modfile: string | *"" // an optional -modfile flag to not use the main go.mod
211224
let gotool = [
212225
if #modfile != "" {
213-
"go tool -modfile=\(#modfile)"
226+
"go tool -modfile=\(#modfile)"
214227
},
215228
"go tool",
216229
][0]

internal/ci/github/trybot.cue

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ workflows: trybot: _repo.bashWorkflow & {
3737

3838
jobs: {
3939
test: {
40-
strategy: _testStrategy
41-
"runs-on": "${{ matrix.runner }}"
40+
strategy: {
41+
"fail-fast": false
42+
matrix: {
43+
(_repo.matrixRunnerName): [_repo.linuxMachine, _repo.macosMachine, _repo.windowsMachine]
44+
(_repo.matrixGoVersionName): [_repo.previousGo, _repo.latestGo]
45+
}
46+
}
47+
"runs-on": "${{ \(_repo.matrixRunnerExpr) }}"
4248

4349
let installGo = _repo.installGo & {
44-
#setupGo: with: "go-version": goVersionVal
50+
#setupGo: with: "go-version": "${{ \(_repo.matrixGoVersionExpr) }}"
4551
_
4652
}
4753

@@ -60,10 +66,10 @@ workflows: trybot: _repo.bashWorkflow & {
6066
_repo.earlyChecks & {
6167
// These checks don't vary based on the Go version or OS,
6268
// so we only need to run them on one of the matrix jobs.
63-
if: _isLatestLinux
69+
if: _repo.isLatestGoLinux
6470
},
6571
_goTest & {
66-
if: "\(_repo.isProtectedBranch) || !\(_isLatestLinux)"
72+
if: "\(_repo.isProtectedBranch) || !\(_repo.isLatestGoLinux)"
6773
},
6874
_goTestRace,
6975
_goTest32bit,
@@ -82,30 +88,12 @@ workflows: trybot: _repo.bashWorkflow & {
8288
}
8389
}
8490

85-
let matrixRunner = "matrix.runner"
86-
let goVersion = "matrix.go-version"
87-
let goVersionVal = "${{ \(goVersion) }}"
88-
89-
_testStrategy: {
90-
"fail-fast": false
91-
matrix: {
92-
"go-version": [_repo.previousGo, _repo.latestGo]
93-
runner: [_repo.linuxMachine, _repo.macosMachine, _repo.windowsMachine]
94-
}
95-
}
96-
97-
// _isLatestLinux returns a GitHub expression that evaluates to true if the job
98-
// is running on Linux with the latest version of Go. This expression is often
99-
// used to run certain steps just once per CI workflow, to avoid duplicated
100-
// work.
101-
_isLatestLinux: "(\(goVersion) == '\(_repo.latestGo)' && \(matrixRunner) == '\(_repo.linuxMachine)')"
102-
10391
_goGenerate: githubactions.#Step & {
10492
name: "Generate"
10593
run: "go generate ./..."
10694
// The Go version corresponds to the precise version specified in
10795
// the matrix. Skip windows for now until we work out why re-gen is flaky
108-
if: _isLatestLinux
96+
if: _repo.isLatestGoLinux
10997
}
11098

11199
_goTest: githubactions.#Step & {
@@ -117,7 +105,7 @@ workflows: trybot: _repo.bashWorkflow & {
117105
// The end-to-end tests require a github token secret and are a bit slow,
118106
// so we only run them on pushes to protected branches and on one
119107
// environment in the source repo.
120-
if: "github.repository == '\(_repo.githubRepositoryPath)' && (\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_isLatestLinux)"
108+
if: "github.repository == '\(_repo.githubRepositoryPath)' && (\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_repo.isLatestGoLinux)"
121109
}] & [
122110
// Two setup steps per the upstream docs:
123111
// https://github.com/google-github-actions/setup-gcloud#service-account-key-json
@@ -157,15 +145,15 @@ workflows: trybot: _repo.bashWorkflow & {
157145
// However, CUE does not have any such build tags yet, and we don't use
158146
// dependencies that vary wildly between platforms.
159147
// For now, to save CI resources, just run the checks on one matrix job.
160-
if: _isLatestLinux
148+
if: _repo.isLatestGoLinux
161149
}] & [
162150
_repo.goChecks,
163151
{
164152
name: "Verify the end-to-end tests still build"
165153
// Ensure that the end-to-end tests in ./internal/_e2e, which are only run
166154
// on pushes to protected branches, still build correctly before merging.
167155
"working-directory": "./internal/_e2e"
168-
run: "go test -run=-"
156+
run: "go test -run=-"
169157
},
170158
// Note that we don't want tooling dependencies in the go.mod file,
171159
// given how many downstreams rely on the cue module having few dependencies.
@@ -179,7 +167,7 @@ workflows: trybot: _repo.bashWorkflow & {
179167
// We use `git ls-remote` to list all tags from each remote git repository
180168
// because it does not depend on custom REST API endpoints and is very fast.
181169
// Note that it sorts tag names as strings, which is not the best, but works OK.
182-
if: "(\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_isLatestLinux)"
170+
if: "(\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_repo.isLatestGoLinux)"
183171
name: "Check all git tags are available"
184172
run: """
185173
cd $(mktemp -d)
@@ -202,7 +190,7 @@ workflows: trybot: _repo.bashWorkflow & {
202190
// Windows and Mac on CI are slower than Linux, and most data races are not specific
203191
// to any OS or Go version in particular, so only run all tests with -race on Linux
204192
// to not slow down CI unnecessarily.
205-
if: _isLatestLinux
193+
if: _repo.isLatestGoLinux
206194
name: "Test with -race"
207195
env: GORACE: "atexit_sleep_ms=10" // Otherwise every Go package being tested sleeps for 1s; see https://go.dev/issues/20364.
208196
run: "go test -race ./..."
@@ -218,7 +206,7 @@ workflows: trybot: _repo.bashWorkflow & {
218206
// We skip this step when testing CLs and PRs, as Linux on the latest Go is the slowest
219207
// job in the matrix due to the use of `go test -race`. 32-bit bugs should be rare,
220208
// so them only getting caught once a patch is merged into master is not a big problem.
221-
if: "(\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_isLatestLinux)"
209+
if: "(\(_repo.isProtectedBranch) || \(_repo.isTestDefaultBranch)) && \(_repo.isLatestGoLinux)"
222210
name: "Test on 32 bits"
223211
env: GOARCH: "386"
224212
run: "go test -short ./..."

0 commit comments

Comments
 (0)