forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_runner_label.go
340 lines (305 loc) · 9.17 KB
/
rule_runner_label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package actionlint
import (
"path"
"strings"
)
type runnerOSCompat uint
const (
compatInvalid = 0
compatUbuntu2004 runnerOSCompat = 1 << iota
compatUbuntu2204
compatUbuntu2404
compatMacOS110
compatMacOS120
compatMacOS120L
compatMacOS120XL
compatMacOS130
compatMacOS130L
compatMacOS130XL
compatMacOS140
compatMacOS140L
compatMacOS140XL
compatWindows2016
compatWindows2019
compatWindows2022
)
// https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
var allGitHubHostedRunnerLabels = []string{
"windows-latest",
"windows-latest-8-cores",
"windows-2022",
"windows-2019",
"windows-2016",
"ubuntu-latest",
"ubuntu-latest-4-cores",
"ubuntu-latest-8-cores",
"ubuntu-latest-16-cores",
"ubuntu-24.04",
"ubuntu-22.04",
"ubuntu-20.04",
"macos-latest",
"macos-latest-xl",
"macos-latest-xlarge",
"macos-latest-large",
"macos-14-xl",
"macos-14-xlarge",
"macos-14-large",
"macos-14",
"macos-14.0",
"macos-13-xl",
"macos-13-xlarge",
"macos-13-large",
"macos-13",
"macos-13.0",
"macos-12-xl",
"macos-12-xlarge",
"macos-12-large",
"macos-12",
"macos-12.0",
"macos-11",
"macos-11.0",
}
// https://docs.github.com/en/actions/hosting-your-own-runners/using-self-hosted-runners-in-a-workflow#using-default-labels-to-route-jobs
var selfHostedRunnerPresetOSLabels = []string{
"linux",
"macos",
"windows",
}
// https://docs.github.com/en/actions/hosting-your-own-runners/using-self-hosted-runners-in-a-workflow#using-default-labels-to-route-jobs
var selfHostedRunnerPresetOtherLabels = []string{
"self-hosted",
"x64",
"arm",
"arm64",
}
var defaultRunnerOSCompats = map[string]runnerOSCompat{
"ubuntu-latest": compatUbuntu2204,
"ubuntu-latest-4-cores": compatUbuntu2204,
"ubuntu-latest-8-cores": compatUbuntu2204,
"ubuntu-latest-16-cores": compatUbuntu2204,
"ubuntu-24.04": compatUbuntu2404,
"ubuntu-22.04": compatUbuntu2204,
"ubuntu-20.04": compatUbuntu2004,
"macos-latest-xl": compatMacOS140XL,
"macos-latest-xlarge": compatMacOS140XL,
"macos-latest-large": compatMacOS140L,
"macos-latest": compatMacOS140,
"macos-14-xl": compatMacOS140XL,
"macos-14-xlarge": compatMacOS140XL,
"macos-14-large": compatMacOS140L,
"macos-14": compatMacOS140,
"macos-14.0": compatMacOS140,
"macos-13-xl": compatMacOS130XL,
"macos-13-xlarge": compatMacOS130XL,
"macos-13-large": compatMacOS130L,
"macos-13": compatMacOS130,
"macos-13.0": compatMacOS130,
"macos-12-xl": compatMacOS120XL,
"macos-12-xlarge": compatMacOS120XL,
"macos-12-large": compatMacOS120L,
"macos-12": compatMacOS120,
"macos-12.0": compatMacOS120,
"macos-11": compatMacOS110,
"macos-11.0": compatMacOS110,
"windows-latest": compatWindows2022,
"windows-latest-8-cores": compatWindows2022,
"windows-2022": compatWindows2022,
"windows-2019": compatWindows2019,
"windows-2016": compatWindows2016,
"linux": compatUbuntu2404 | compatUbuntu2204 | compatUbuntu2004, // Note: "linux" does not always indicate Ubuntu. It might be Fedora or Arch or ...
"macos": compatMacOS140 | compatMacOS140L | compatMacOS140XL | compatMacOS130 | compatMacOS130L | compatMacOS130XL | compatMacOS120 | compatMacOS120L | compatMacOS120XL | compatMacOS110,
"windows": compatWindows2022 | compatWindows2019 | compatWindows2016,
}
// RuleRunnerLabel is a rule to check runner label like "ubuntu-latest". There are two types of
// runners, GitHub-hosted runner and Self-hosted runner. GitHub-hosted runner is described at
// https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners .
// And Self-hosted runner is described at
// https://docs.github.com/en/actions/hosting-your-own-runners/using-self-hosted-runners-in-a-workflow .
type RuleRunnerLabel struct {
RuleBase
// Note: Using only one compatibility integer is enough to check compatibility. But we remember
// all past compatibility values here for better error message. If accumulating all compatibility
// values into one integer, we can no longer know what labels are conflicting.
compats map[runnerOSCompat]*String
}
// NewRuleRunnerLabel creates new RuleRunnerLabel instance.
func NewRuleRunnerLabel() *RuleRunnerLabel {
return &RuleRunnerLabel{
RuleBase: RuleBase{
name: "runner-label",
desc: "Checks for GitHub-hosted and preset self-hosted runner labels in \"runs-on:\"",
},
compats: nil,
}
}
// VisitJobPre is callback when visiting Job node before visiting its children.
func (rule *RuleRunnerLabel) VisitJobPre(n *Job) error {
if n.RunsOn == nil {
return nil
}
var m *Matrix
if n.Strategy != nil {
m = n.Strategy.Matrix
}
if len(n.RunsOn.Labels) == 1 {
rule.checkLabel(n.RunsOn.Labels[0], m)
return nil
}
rule.compats = map[runnerOSCompat]*String{}
if n.RunsOn.LabelsExpr != nil {
rule.checkLabelAndConflict(n.RunsOn.LabelsExpr, m)
} else {
for _, label := range n.RunsOn.Labels {
rule.checkLabelAndConflict(label, m)
}
}
rule.compats = nil // reset
return nil
}
// https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
func (rule *RuleRunnerLabel) checkLabelAndConflict(l *String, m *Matrix) {
if l.ContainsExpression() {
ss := rule.tryToGetLabelsInMatrix(l, m)
cs := make([]runnerOSCompat, 0, len(ss))
for _, s := range ss {
comp := rule.verifyRunnerLabel(s)
cs = append(cs, comp)
}
rule.checkCombiCompat(cs, ss)
return
}
comp := rule.verifyRunnerLabel(l)
rule.checkCompat(comp, l)
}
func (rule *RuleRunnerLabel) checkLabel(l *String, m *Matrix) {
if l.ContainsExpression() {
ss := rule.tryToGetLabelsInMatrix(l, m)
for _, s := range ss {
rule.verifyRunnerLabel(s)
}
return
}
rule.verifyRunnerLabel(l)
}
func (rule *RuleRunnerLabel) verifyRunnerLabel(label *String) runnerOSCompat {
l := label.Value
if c, ok := defaultRunnerOSCompats[strings.ToLower(l)]; ok {
return c
}
for _, p := range selfHostedRunnerPresetOtherLabels {
if strings.EqualFold(l, p) {
return compatInvalid
}
}
known := rule.getKnownLabels()
for _, k := range known {
m, err := path.Match(k, l)
if err != nil {
rule.Errorf(label.Pos, "label pattern %q is an invalid glob. kindly check list of labels in actionlint.yaml config file: %v", k, err)
return compatInvalid
}
if m {
return compatInvalid
}
}
rule.Errorf(
label.Pos,
"label %q is unknown. available labels are %s. if it is a custom label for self-hosted runner, set list of labels in actionlint.yaml config file",
label.Value,
quotesAll(
allGitHubHostedRunnerLabels,
selfHostedRunnerPresetOtherLabels,
selfHostedRunnerPresetOSLabels,
known,
),
)
return compatInvalid
}
func (rule *RuleRunnerLabel) tryToGetLabelsInMatrix(label *String, m *Matrix) []*String {
if m == nil {
return nil
}
// Only when the form of "${{...}}", evaluate the expression
if !label.IsExpressionAssigned() {
return nil
}
l := strings.TrimSpace(label.Value)
p := NewExprParser()
expr, err := p.Parse(NewExprLexer(l[3:])) // 3 means omit first "${{"
if err != nil {
return nil
}
deref, ok := expr.(*ObjectDerefNode)
if !ok {
return nil
}
recv, ok := deref.Receiver.(*VariableNode)
if !ok {
return nil
}
if recv.Name != "matrix" {
return nil
}
prop := deref.Property
labels := []*String{}
if m.Rows != nil {
if row, ok := m.Rows[prop]; ok {
for _, v := range row.Values {
if s, ok := v.(*RawYAMLString); ok && !ContainsExpression(s.Value) {
labels = append(labels, &String{s.Value, false, s.Pos()})
}
}
}
}
if m.Include != nil {
for _, combi := range m.Include.Combinations {
if combi.Assigns != nil {
if assign, ok := combi.Assigns[prop]; ok {
if s, ok := assign.Value.(*RawYAMLString); ok && !ContainsExpression(s.Value) {
labels = append(labels, &String{s.Value, false, s.Pos()})
}
}
}
}
}
return labels
}
func (rule *RuleRunnerLabel) checkConflict(comp runnerOSCompat, label *String) bool {
for c, l := range rule.compats {
if c&comp == 0 {
rule.Errorf(label.Pos, "label %q conflicts with label %q defined at %s. note: to run your job on each workers, use matrix", label.Value, l.Value, l.Pos)
return false
}
}
return true
}
func (rule *RuleRunnerLabel) checkCompat(comp runnerOSCompat, label *String) {
if comp == compatInvalid || !rule.checkConflict(comp, label) {
return
}
if _, ok := rule.compats[comp]; !ok {
rule.compats[comp] = label
}
}
func (rule *RuleRunnerLabel) checkCombiCompat(comps []runnerOSCompat, labels []*String) {
for i, c := range comps {
if c != compatInvalid && !rule.checkConflict(c, labels[i]) {
// Overwrite the compatibility value with compatInvalid at conflicted label not to
// register the label to `rule.compats`.
comps[i] = compatInvalid
}
}
for i, c := range comps {
if c != compatInvalid {
if _, ok := rule.compats[c]; !ok {
rule.compats[c] = labels[i]
}
}
}
}
func (rule *RuleRunnerLabel) getKnownLabels() []string {
if rule.config == nil {
return nil
}
return rule.config.SelfHostedRunner.Labels
}