| 1 |
// Copyright 2022 The CUE Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package github |
| 16 |
|
| 17 |
import ( |
| 18 |
"list" |
| 19 |
"path" |
| 20 |
|
| 21 |
"github.com/SchemaStore/schemastore/src/schemas/json" |
| 22 |
) |
| 23 |
|
| 24 |
// The trybot workflow. |
| 25 |
workflows: trybot: _repo.bashWorkflow & { |
| 26 |
name: _repo.trybot.name |
| 27 |
|
| 28 |
on: { |
| 29 |
push: { |
| 30 |
branches: list.Concat([[_repo.testDefaultBranch], _repo.protectedBranchPatterns]) // do not run PR branches |
| 31 |
} |
| 32 |
pull_request: {} |
| 33 |
} |
| 34 |
|
| 35 |
jobs: { |
| 36 |
test: { |
| 37 |
"runs-on": _repo.linuxMachine |
| 38 |
|
| 39 |
let runnerOSExpr = "runner.os" |
| 40 |
let runnerOSVal = "${{ \(runnerOSExpr) }}" |
| 41 |
let installGo = _repo.installGo & { |
| 42 |
#setupGo: with: "go-version": _repo.latestGo |
| 43 |
_ |
| 44 |
} |
| 45 |
let _setupGoActionsCaches = _repo.setupGoActionsCaches & { |
| 46 |
#goVersion: _repo.latestGo |
| 47 |
#os: runnerOSVal |
| 48 |
_ |
| 49 |
} |
| 50 |
|
| 51 |
// Only run the trybot workflow if we have the trybot trailer, or |
| 52 |
// if we have no special trailers. Note this condition applies |
| 53 |
// after and in addition to the "on" condition above. |
| 54 |
if: "\(_repo.containsTrybotTrailer) || ! \(_repo.containsDispatchTrailer)" |
| 55 |
|
| 56 |
steps: [ |
| 57 |
for v in _repo.checkoutCode {v}, |
| 58 |
|
| 59 |
for v in installGo {v}, |
| 60 |
for v in _setupGoActionsCaches {v}, |
| 61 |
|
| 62 |
_repo.earlyChecks, |
| 63 |
|
| 64 |
for _, v in perModuleChecks {v}, |
| 65 |
|
| 66 |
// Run "go work sync" after we have checked and tested every module. |
| 67 |
// This way, if a "go test" command fails, it is much easier for the developer |
| 68 |
// to reproduce on their machine without having to remember "go work sync". |
| 69 |
// If "go work sync" makes any changes, then the git clean check below will fail anyway. |
| 70 |
json.#step & { |
| 71 |
run: "go work sync" |
| 72 |
}, |
| 73 |
|
| 74 |
_repo.checkGitClean, |
| 75 |
] |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
let perModuleChecks = [ |
| 80 |
for _, goModPath in _repo.modules |
| 81 |
let modDir = path.Dir(goModPath) |
| 82 |
let modIsInternal = _#goModDirIsInternal & {#goModDir: modDir, _} |
| 83 |
for _, gowork in ["", if !modIsInternal {"off"}] |
| 84 |
for _, step in [_#goGenerate, _#goTest, _#goCheck] { |
| 85 |
step & { |
| 86 |
#name: modDir + [if gowork != "" {" with GOWORK=\(gowork)"}, ""][0] |
| 87 |
"working-directory": modDir |
| 88 |
env: { |
| 89 |
GOWORK: gowork |
| 90 |
} |
| 91 |
} |
| 92 |
}, |
| 93 |
] |
| 94 |
|
| 95 |
// _#goModIsInternal determins whether a repo root-relative directory path |
| 96 |
// to a go.mod filepath is internal from a Go modules perspective. |
| 97 |
_#goModDirIsInternal: { |
| 98 |
// #path is the repo root-relative directory path containing a go.mod |
| 99 |
// file |
| 100 |
#goModDir: string |
| 101 |
|
| 102 |
let pathElems = path.Split(#goModDir) |
| 103 |
let isInternal = [for _, v in pathElems {v == "internal"}] |
| 104 |
_res: [for i, v in isInternal {[if i == 0 {false}, _res[i-1]][0] || v}] |
| 105 |
|
| 106 |
// In case the root is a module |
| 107 |
*_res[0] | false |
| 108 |
} |
| 109 |
|
| 110 |
_#goGenerate: json.#step & { |
| 111 |
#name: string |
| 112 |
name: "Generate \(#name)" |
| 113 |
run: "go generate ./..." |
| 114 |
} |
| 115 |
|
| 116 |
_#goTest: json.#step & { |
| 117 |
#name: string |
| 118 |
name: "Test \(#name)" |
| 119 |
run: "go test ./..." |
| 120 |
} |
| 121 |
|
| 122 |
_#goCheck: json.#step & { |
| 123 |
// These checks can vary between platforms, as different code can be built |
| 124 |
// based on GOOS and GOARCH build tags. |
| 125 |
// However, CUE does not have any such build tags yet, and we don't use |
| 126 |
// dependencies that vary wildly between platforms. |
| 127 |
// For now, to save CI resources, just run the checks on one matrix job. |
| 128 |
// TODO: consider adding more checks as per https://github.com/golang/go/issues/42119. |
| 129 |
#name: string |
| 130 |
name: "Check \(#name)" |
| 131 |
run: "go vet ./..." |
| 132 |
} |
| 133 |
} |