Skip to content

Commit

Permalink
internal/ci: hoist cache steps to base package
Browse files Browse the repository at this point in the history
The steps currently declared in workflows.cue can be made common to any
workflow that requires Go caching. We will, for example, use this in
other CUE repos. Hoist a definition that encapsulates these steps.

This change should not result in any .github/workflows changes.

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I2a0ba0a12a13c72c701157d138ac24fab3b024a2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551350
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
myitcv committed Mar 25, 2023
1 parent 57066d4 commit 019f6dd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
54 changes: 54 additions & 0 deletions internal/ci/base/base.cue
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,57 @@ let _#botGitHubUserTokenSecretsKey = #botGitHubUserTokenSecretsKey
let parts = strings.Split(#url, "/")
strings.Join(list.Slice(parts, 3, len(parts)), "/")
}

#setupGoActionsCaches: {
// #protectedBranchExpr is a GitHub expression
// (https://docs.github.com/en/actions/learn-github-actions/expressions)
// that evaluates to true if the workflow is running for a commit against a
// protected branch.
#protectedBranchExpr: string

let goModCacheDirID = "go-mod-cache-dir"
let goCacheDirID = "go-cache-dir"

// cacheDirs is a convenience variable that includes
// GitHub expressions that represent the directories
// that participate in Go caching.
let cacheDirs = [ "${{ steps.\(goModCacheDirID).outputs.dir }}/cache/download", "${{ steps.\(goCacheDirID).outputs.dir }}"]

// pre is the list of steps required to establish and initialise the correct
// caches for Go-based workflows.
[
json.#step & {
name: "Get go mod cache directory"
id: goModCacheDirID
run: #"echo "dir=$(go env GOMODCACHE)" >> ${GITHUB_OUTPUT}"#
},
json.#step & {
name: "Get go build/test cache directory"
id: goCacheDirID
run: #"echo "dir=$(go env GOCACHE)" >> ${GITHUB_OUTPUT}"#
},
for _, v in [
{
if: #protectedBranchExpr
uses: "actions/cache@v3"
},
{
if: "! \(#protectedBranchExpr)"
uses: "actions/cache/restore@v3"
},
] {
v & json.#step & {
with: {
path: strings.Join(cacheDirs, "\n")

// GitHub actions caches are immutable. Therefore, use a key which is
// unique, but allow the restore to fallback to the most recent cache.
// The result is then saved under the new key which will benefit the
// next build
key: "${{ runner.os }}-${{ matrix.go-version }}-${{ github.run_id }}"
"restore-keys": "${{ runner.os }}-${{ matrix.go-version }}"
}
}
},
]
}
5 changes: 4 additions & 1 deletion internal/ci/github/trybot.cue
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ workflows: trybot: _base.#bashWorkflow & {
test: {
strategy: _#testStrategy
"runs-on": "${{ matrix.os }}"

let goCaches = _base.#setupGoActionsCaches & {#protectedBranchExpr: _#isProtectedBranch, _}

steps: [
for v in _base.#checkoutCode {v},
_base.#installGo,

// cachePre must come after installing Node and Go, because the cache locations
// are established by running each tool.
for v in _#cachePre {v},
for v in goCaches {v},

// All tests on protected branches should skip the test cache.
// The canonical way to do this is with -count=1. However, we
Expand Down
38 changes: 0 additions & 38 deletions internal/ci/github/workflows.cue
Original file line number Diff line number Diff line change
Expand Up @@ -130,41 +130,3 @@ _base: base & {
#botGitHubUser: "cueckoo"
#botGitHubUserTokenSecretsKey: "CUECKOO_GITHUB_PAT"
}

_#cacheDirs: [ "${{ steps.go-mod-cache-dir.outputs.dir }}/cache/download", "${{ steps.go-cache-dir.outputs.dir }}"]

_#cachePre: [
json.#step & {
name: "Get go mod cache directory"
id: "go-mod-cache-dir"
run: #"echo "dir=$(go env GOMODCACHE)" >> ${GITHUB_OUTPUT}"#
},
json.#step & {
name: "Get go build/test cache directory"
id: "go-cache-dir"
run: #"echo "dir=$(go env GOCACHE)" >> ${GITHUB_OUTPUT}"#
},
for _, v in [
{
if: _#isProtectedBranch
uses: "actions/cache@v3"
},
{
if: "! \(_#isProtectedBranch)"
uses: "actions/cache/restore@v3"
},
] {
v & json.#step & {
with: {
path: strings.Join(_#cacheDirs, "\n")

// GitHub actions caches are immutable. Therefore, use a key which is
// unique, but allow the restore to fallback to the most recent cache.
// The result is then saved under the new key which will benefit the
// next build
key: "${{ runner.os }}-${{ matrix.go-version }}-${{ github.run_id }}"
"restore-keys": "${{ runner.os }}-${{ matrix.go-version }}"
}
}
},
]

0 comments on commit 019f6dd

Please sign in to comment.