Skip to content

Commit

Permalink
internal/ci: move workflows.cue defs to better places
Browse files Browse the repository at this point in the history
workflows.cue, and hence the top level namespace of the github package,
has long been a bit of a dumping ground for "things that are common to
all workflows". Indeed it has grown to include a few things that should
even be part of the base package.

Split up workflows.cue accordingly. This is a purely mechanical change.

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

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I53f19e643aaf8b710ada5a2bbdf9ff9ddcb43dd6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551354
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
myitcv committed Mar 25, 2023
1 parent 019f6dd commit 0250807
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 73 deletions.
57 changes: 57 additions & 0 deletions internal/ci/base/base.cue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
#testDefaultBranch: "ci/test"
#botGitHubUser: string
#botGitHubUserTokenSecretsKey: string
#protectedBranchPatterns: [...string]
#releaseTagPattern: string

#doNotEditMessage: {
#generatedBy: string
Expand Down Expand Up @@ -272,3 +274,58 @@ let _#botGitHubUserTokenSecretsKey = #botGitHubUserTokenSecretsKey
},
]
}

// #codeReview defines the schema of a codereview.cfg file that
// sits at the root of a repository. codereview.cfg is the configuration
// file that drives golang.org/x/review/git-codereview. This config
// file is also used by github.com/cue-sh/tools/cmd/cueckoo.
#codeReview: {
gerrit?: string
github?: string
"cue-unity"?: string
}

// #toCodeReviewCfg converts a #codeReview instance to
// the key: value
#toCodeReviewCfg: {
#input: #codeReview
let parts = [ for k, v in #input {k + ": " + v}]

// Per https://pkg.go.dev/golang.org/x/review/git-codereview#hdr-Configuration
strings.Join(parts, "\n")
}

// _#matchPattern returns a GitHub Actions expression which evaluates whether a
// variable matches a globbing pattern. For literal patterns it uses "==",
// and for suffix patterns it uses "startsWith".
// See https://docs.github.com/en/actions/learn-github-actions/expressions.
_#matchPattern: {
variable: string
pattern: string
expr: [
if strings.HasSuffix(pattern, "*") {
let prefix = strings.TrimSuffix(pattern, "*")
"startsWith(\(variable), '\(prefix)')"
},
{
"\(variable) == '\(pattern)'"
},
][0]
}

// #isProtectedBranch is an expression that evaluates to true if the
// job is running as a result of pushing to one of _#protectedBranchPatterns.
// It would be nice to use the "contains" builtin for simplicity,
// but array literals are not yet supported in expressions.
#isProtectedBranch: {
"(" + strings.Join([ for branch in #protectedBranchPatterns {
(_#matchPattern & {variable: "github.ref", pattern: "refs/heads/\(branch)"}).expr
}], " || ") + ")"
}

// #isReleaseTag creates a GitHub expression, based on the given release tag
// pattern, that evaluates to true if called in the context of a workflow that
// is part of a release.
#isReleaseTag: {
(_#matchPattern & {variable: "github.ref", pattern: "refs/tags/\(#releaseTagPattern)"}).expr
}
18 changes: 18 additions & 0 deletions internal/ci/core/core.cue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"cuelang.org/go/internal/ci/base"
)

// The machines that we use
linuxMachine: "ubuntu-22.04"
macosMachine: "macos-11"
windowsMachine: "windows-2022"

// Define core URLs that will be used in the codereview.cfg and GitHub workflows
githubRepositoryURL: "https://github.com/cue-lang/cue"
gerritRepositoryURL: "https://review.gerrithub.io/a/cue-lang/cue"
Expand Down Expand Up @@ -63,3 +68,16 @@ codeReview: base.#codeReview & {
gerrit: gerritRepositoryURL
"cue-unity": unityRepositoryURL
}

// protectedBranchPatterns is a list of glob patterns to match the protected
// git branches which are continuously used during development on Gerrit.
// This includes the default branch and release branches,
// but excludes any others like feature branches or short-lived branches.
// Note that ci/test is excluded as it is GitHub-only.
protectedBranchPatterns: [defaultBranch, releaseBranchPattern]

// isLatestLinux returns a GitHub expression that evaluates to true if the job
// is running on Linux with the latest version of Go. This expression is often
// used to run certain steps just once per CI workflow, to avoid duplicated
// work.
isLatestLinux: "(matrix.go-version == '\(latestStableGo)' && matrix.os == '\(linuxMachine)')"
4 changes: 2 additions & 2 deletions internal/ci/github/evict_caches.cue
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ workflows: evict_caches: _base.#bashWorkflow & {
test: {
// We only want to run this in the main repo
if: "${{github.repository == '\(core.githubRepositoryPath)'}}"
"runs-on": _#linuxMachine
"runs-on": core.linuxMachine
steps: [
json.#step & {
let branchPatterns = strings.Join(_#protectedBranchPatterns, " ")
let branchPatterns = strings.Join(core.protectedBranchPatterns, " ")

// rerunLatestWorkflow runs the latest trybot workflow in the
// specified repo for branches that match the specified branch.
Expand Down
4 changes: 2 additions & 2 deletions internal/ci/github/push_tip_to_trybot.cue
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ workflows: push_tip_to_trybot: _base.#bashWorkflow & {

name: "Push tip to trybot"
on: {
push: branches: _#protectedBranchPatterns
push: branches: core.protectedBranchPatterns
}

concurrency: "push_tip_to_trybot"

jobs: push: {
"runs-on": _#linuxMachine
"runs-on": core.linuxMachine
if: "${{github.repository == '\(core.githubRepositoryPath)'}}"
steps: [
_gerrithub.#writeNetrcFile,
Expand Down
8 changes: 4 additions & 4 deletions internal/ci/github/release.cue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ workflows: release: _base.#bashWorkflow & {

on: push: {
tags: [core.releaseTagPattern, "!" + core.zeroReleaseTagPattern]
branches: list.Concat([[_base.#testDefaultBranch], _#protectedBranchPatterns])
branches: list.Concat([[_base.#testDefaultBranch], core.protectedBranchPatterns])
}
jobs: goreleaser: {
"runs-on": _#linuxMachine
"runs-on": core.linuxMachine
if: "${{github.repository == '\(core.githubRepositoryPath)'}}"
steps: [
for v in _base.#checkoutCode {v},
Expand Down Expand Up @@ -87,15 +87,15 @@ workflows: release: _base.#bashWorkflow & {
},
_base.#repositoryDispatch & {
name: "Re-test cuelang.org"
if: _#isReleaseTag
if: _base.#isReleaseTag
#repositoryURL: "https://github.com/cue-lang/cuelang.org"
#arg: {
event_type: "Re-test post release of \(_#cueVersionRef)"
}
},
_base.#repositoryDispatch & {
name: "Trigger unity build"
if: _#isReleaseTag
if: _base.#isReleaseTag
#repositoryURL: core.unityRepositoryURL
#arg: {
event_type: "Check against CUE \(_#cueVersionRef)"
Expand Down
2 changes: 1 addition & 1 deletion internal/ci/github/tip_triggers.cue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ workflows: tip_triggers: _base.#bashWorkflow & {
name: "Triggers on push to tip"
on: push: branches: [core.defaultBranch]
jobs: push: {
"runs-on": _#linuxMachine
"runs-on": core.linuxMachine
if: "${{github.repository == '\(core.githubRepositoryPath)'}}"
steps: [
_base.#repositoryDispatch & {
Expand Down
28 changes: 18 additions & 10 deletions internal/ci/github/trybot.cue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ workflows: trybot: _base.#bashWorkflow & {

on: {
push: {
branches: list.Concat([["trybot/*/*", _base.#testDefaultBranch], _#protectedBranchPatterns]) // do not run PR branches
branches: list.Concat([["trybot/*/*", _base.#testDefaultBranch], core.protectedBranchPatterns]) // do not run PR branches
"tags-ignore": [core.releaseTagPattern]
}
pull_request: {}
Expand All @@ -46,7 +46,7 @@ workflows: trybot: _base.#bashWorkflow & {
strategy: _#testStrategy
"runs-on": "${{ matrix.os }}"

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

steps: [
for v in _base.#checkoutCode {v},
Expand All @@ -62,25 +62,25 @@ workflows: trybot: _base.#bashWorkflow & {
// subsequent CLs in the trybot repo can leverage the updated
// cache. Therefore, we instead perform a clean of the testcache.
json.#step & {
if: "github.repository == '\(core.githubRepositoryPath)' && (\(_#isProtectedBranch) || github.ref == 'refs/heads/\(_base.#testDefaultBranch)')"
if: "github.repository == '\(core.githubRepositoryPath)' && (\(_base.#isProtectedBranch) || github.ref == 'refs/heads/\(_base.#testDefaultBranch)')"
run: "go clean -testcache"
},

_base.#earlyChecks & {
// These checks don't vary based on the Go version or OS,
// so we only need to run them on one of the matrix jobs.
if: _#isLatestLinux
if: core.isLatestLinux
},
json.#step & {
if: "\(_#isProtectedBranch) || \(_#isLatestLinux)"
if: "\(_base.#isProtectedBranch) || \(core.isLatestLinux)"
run: "echo CUE_LONG=true >> $GITHUB_ENV"
},
_#goGenerate,
_#goTest & {
if: "\(_#isProtectedBranch) || !\(_#isLatestLinux)"
if: "\(_base.#isProtectedBranch) || !\(core.isLatestLinux)"
},
_#goTestRace & {
if: _#isLatestLinux
if: core.isLatestLinux
},
_#goCheck,
_base.#checkGitClean,
Expand All @@ -89,6 +89,14 @@ workflows: trybot: _base.#bashWorkflow & {
}
}

_#testStrategy: {
"fail-fast": false
matrix: {
"go-version": ["1.18.x", core.latestStableGo]
os: [core.linuxMachine, core.macosMachine, core.windowsMachine]
}
}

_#pullThroughProxy: json.#step & {
name: "Pull this commit through the proxy on \(core.defaultBranch)"
run: """
Expand Down Expand Up @@ -122,15 +130,15 @@ workflows: trybot: _base.#bashWorkflow & {
echo "giving up after a number of retries"
exit 1
"""
if: "\(_#isProtectedBranch) && \(_#isLatestLinux)"
if: "\(_base.#isProtectedBranch) && \(core.isLatestLinux)"
}

_#goGenerate: json.#step & {
name: "Generate"
run: "go generate ./..."
// The Go version corresponds to the precise version specified in
// the matrix. Skip windows for now until we work out why re-gen is flaky
if: _#isLatestLinux
if: core.isLatestLinux
}

_#goTest: json.#step & {
Expand All @@ -145,7 +153,7 @@ workflows: trybot: _base.#bashWorkflow & {
// dependencies that vary wildly between platforms.
// For now, to save CI resources, just run the checks on one matrix job.
// TODO: consider adding more checks as per https://github.com/golang/go/issues/42119.
if: "\(_#isLatestLinux)"
if: "\(core.isLatestLinux)"
name: "Check"
run: "go vet ./..."
}
Expand Down
56 changes: 2 additions & 54 deletions internal/ci/github/workflows.cue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package github

import (
"strings"

"cuelang.org/go/internal/ci/core"
"cuelang.org/go/internal/ci/base"
"cuelang.org/go/internal/ci/gerrithub"
Expand Down Expand Up @@ -54,58 +52,6 @@ workflows: close({
evict_caches: _
})

// _#protectedBranchPatterns is a list of glob patterns to match the protected
// git branches which are continuously used during development on Gerrit.
// This includes the default branch and release branches,
// but excludes any others like feature branches or short-lived branches.
// Note that #testDefaultBranch is excluded as it is GitHub-only.
_#protectedBranchPatterns: [core.defaultBranch, core.releaseBranchPattern]

// _#matchPattern returns a GitHub Actions expression which evaluates whether a
// variable matches a globbing pattern. For literal patterns it uses "==",
// and for suffix patterns it uses "startsWith".
// See https://docs.github.com/en/actions/learn-github-actions/expressions.
_#matchPattern: {
variable: string
pattern: string
expr: [
if strings.HasSuffix(pattern, "*") {
let prefix = strings.TrimSuffix(pattern, "*")
"startsWith(\(variable), '\(prefix)')"
},
{
"\(variable) == '\(pattern)'"
},
][0]
}

// _#isProtectedBranch is an expression that evaluates to true if the
// job is running as a result of pushing to one of _#protectedBranchPatterns.
// It would be nice to use the "contains" builtin for simplicity,
// but array literals are not yet supported in expressions.
_#isProtectedBranch: "(" + strings.Join([ for branch in _#protectedBranchPatterns {
(_#matchPattern & {variable: "github.ref", pattern: "refs/heads/\(branch)"}).expr
}], " || ") + ")"

_#isReleaseTag: (_#matchPattern & {variable: "github.ref", pattern: "refs/tags/\(core.releaseTagPattern)"}).expr

_#linuxMachine: "ubuntu-22.04"
_#macosMachine: "macos-11"
_#windowsMachine: "windows-2022"

// _#isLatestLinux evaluates to true if the job is running on Linux with the
// latest version of Go. This expression is often used to run certain steps
// just once per CI workflow, to avoid duplicated work.
_#isLatestLinux: "(matrix.go-version == '\(core.latestStableGo)' && matrix.os == '\(_#linuxMachine)')"

_#testStrategy: {
"fail-fast": false
matrix: {
"go-version": ["1.18.x", core.latestStableGo]
os: [_#linuxMachine, _#macosMachine, _#windowsMachine]
}
}

// _gerrithub is an instance of ./gerrithub, parameterised by the properties of
// this project
_gerrithub: gerrithub & {
Expand All @@ -129,4 +75,6 @@ _base: base & {
#defaultBranch: core.defaultBranch
#botGitHubUser: "cueckoo"
#botGitHubUserTokenSecretsKey: "CUECKOO_GITHUB_PAT"
#protectedBranchPatterns: core.protectedBranchPatterns
#releaseTagPattern: core.releaseTagPattern
}

0 comments on commit 0250807

Please sign in to comment.