-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
293 lines (263 loc) · 6.91 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"github.com/jjs-dev/ci-config-gen/actions"
"github.com/jjs-dev/ci-config-gen/bors"
"github.com/jjs-dev/ci-config-gen/config"
"github.com/jjs-dev/ci-config-gen/languages"
"gopkg.in/yaml.v2"
)
func preprocessWorkflow(workflow actions.Workflow) actions.Workflow {
err := workflow.Validate()
if err != nil {
log.Fatalf("Workflow %s in invalid: %v", workflow.Name, err)
}
// no modifications currently
return workflow
}
func emitFile(out string, relName string, data []byte) {
fullPath := path.Join(out, relName)
err := os.WriteFile(fullPath, data, 0o755)
if err != nil {
log.Fatalf("failed to write %s: %v", relName, err)
}
}
func writeWorkflow(out string, workflow actions.Workflow) {
alert := "# GENERATED FILE DO NOT EDIT\n"
y, err := yaml.Marshal(preprocessWorkflow(workflow))
data := append([]byte(alert), y...)
if err != nil {
log.Fatalf("failed to serialize workflow %v", err)
}
emitFile(out, fmt.Sprintf(".github/workflows/%s.yaml", workflow.Name), data)
}
func main() {
repoRoot := flag.String("repo-root", "", "path to root directory of the repository to generate config for")
out := flag.String("output", "", "directory which will contain generated workflow files. defaults to $(repo-root)")
flag.Parse()
if *repoRoot == "" {
log.Fatal("--repo-root not provided")
}
if *out == "" {
*out = *repoRoot
}
config, err := config.Load(*repoRoot)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
log.Printf("loaded config: %+v", config)
borsConfig := &bors.BorsConfig{}
borsConfig.ApplyDefaults()
borsConfig.Timeout = config.BuildTimeout * 60
metaWorkflow := makeMetaWorkflow(borsConfig, config)
writeWorkflow(*out, metaWorkflow)
langs := languages.MakeLanguages()
for _, lang := range langs {
log.Printf("Generating files for lang %s\n", lang.Name())
err = lang.WriteAdditionalFiles(*repoRoot)
if err != nil {
log.Fatalf("failed to write additional files: %v", err)
}
}
ciWorkflow := makeCiWorkflow(langs, config, *repoRoot, borsConfig)
writeWorkflow(*out, ciWorkflow)
if !config.NoPublish {
log.Println("Generating publish workflow")
publishWorkflow := makePublishWorkflow(*repoRoot, config, borsConfig)
writeWorkflow(*out, publishWorkflow)
script := generatePublishImageScript(config)
emitFile(*out, "ci/publish-images.sh", []byte(script))
}
log.Println("Generating bors config")
borsConfigBytes, err := borsConfig.Serialize()
if err != nil {
log.Fatal(err)
}
emitFile(*out, "bors.toml", borsConfigBytes)
}
func makeMetaWorkflow(bc *bors.BorsConfig, cfg config.CiConfig) actions.Workflow {
bc.AddJob("check-ci-config")
var fetchGenerator actions.Step
var generatorLocation string
if cfg.InternalHackForGenerator {
fetchGenerator = actions.Step{
Name: "No-op",
Run: "echo OK",
}
generatorLocation = "."
} else {
fetchGenerator = actions.Step{
Name: "Fetch generator sources",
Run: "git clone https://github.com/jjs-dev/ci-config-gen ./gen",
}
generatorLocation = "./gen"
}
jobs := map[string]actions.Job{
"check-ci-config": {
RunsOn: actions.UbuntuRunner,
Timeout: 1,
Steps: []actions.Step{
actions.MakeCheckoutStep(),
languages.MakeSetupGoStep(),
fetchGenerator,
{
Run: fmt.Sprintf("cd %s && go install -v .", generatorLocation),
Name: "Install ci-config-gen",
},
{
Run: "ci-config-gen --repo-root .",
Name: "Run co-config-gen",
},
{
Name: "Verify CI configuration is up-to-date",
Run: "git diff --exit-code",
},
},
},
}
if cfg.Codegen {
jobs["check-codegen"] = actions.Job{
RunsOn: actions.UbuntuRunner,
Timeout: cfg.JobTimeout,
Steps: []actions.Step{
actions.MakeCheckoutStep(),
{
Name: "Run top-level codegen script",
Run: "bash ci/codegen.sh",
},
{
Name: "Verify generated code is up-to-date",
Run: "git diff --exit-code",
},
},
}
}
return actions.Workflow{
Name: "meta",
On: actions.Trigger{
PullRequest: actions.EmptyStruct{},
Push: actions.PushTrigger{
Branches: []string{"staging", "trying", "master"},
},
},
Jobs: jobs,
}
}
func makeCiE2eJob(root string, config config.CiConfig, languages []languages.Language) (actions.Job, actions.Job) {
buildSteps := []actions.Step{
actions.MakeCheckoutStep(),
}
for _, lang := range languages {
if lang.Used(root) {
needsCache, cacheStep := lang.MakeE2eCacheStep()
if !needsCache {
continue
}
buildSteps = append(buildSteps, cacheStep)
}
}
buildSteps = append(buildSteps, actions.Step{
Name: "Build e2e artifacts",
Run: "bash ci/e2e-build.sh",
}, actions.Step{
Name: "Upload e2e artifacts",
Uses: "actions/upload-artifact@v2",
With: map[string]string{
"name": "e2e-artifacts",
"path": "e2e-artifacts",
"retention-days": "2",
},
})
build := actions.Job{
RunsOn: actions.UbuntuRunner,
Steps: buildSteps,
Timeout: config.JobTimeout,
Env: map[string]string{
"DOCKER_BUILDKIT": "1",
},
}
run := actions.Job{
RunsOn: actions.UbuntuRunner,
Needs: "e2e-build",
Timeout: config.JobTimeout,
Steps: []actions.Step{
actions.MakeCheckoutStep(),
{
Name: "Download e2e artifacts",
Uses: "actions/download-artifact@v2",
With: map[string]string{
"name": "e2e-artifacts",
"path": "e2e-artifacts",
},
},
{
Name: "Execute tests",
Run: "bash ci/e2e-run.sh",
},
{
Name: "Upload logs",
Uses: "actions/upload-artifact@v2",
If: "always()",
With: map[string]string{
"name": "e2e-logs",
"path": "e2e-logs",
"retention-days": "2",
},
},
},
}
return build, run
}
func makeCiWorkflow(langs []languages.Language, config config.CiConfig, repoRoot string, bc *bors.BorsConfig) actions.Workflow {
w := actions.Workflow{
Name: "ci",
On: actions.Trigger{
PullRequest: actions.EmptyStruct{},
Push: actions.PushTrigger{
Branches: []string{"staging", "trying", "master"},
},
},
Jobs: map[string]actions.Job{
"misspell": {
RunsOn: actions.UbuntuRunner,
Timeout: 2,
Steps: []actions.Step{
actions.MakeCheckoutStep(),
{
Name: "run spellcheck",
Uses: "reviewdog/action-misspell@v1",
With: map[string]string{
"github_token": "${{ secrets.GITHUB_TOKEN }}",
"locale": "US",
},
},
},
},
},
}
perLanguageJobs := make([]languages.JobSet, 0)
for _, lang := range langs {
if lang.Used(repoRoot) {
log.Printf("Generating %s CI jobs", lang.Name())
perLanguageJobs = append(perLanguageJobs, lang.Make(repoRoot, config))
}
}
if !config.NoE2e {
e2eBuild, e2eRun := makeCiE2eJob(repoRoot, config, langs)
bc.AddJob("e2e-build")
bc.AddJob("e2e-run")
w.Jobs["e2e-build"] = e2eBuild
w.Jobs["e2e-run"] = e2eRun
}
for _, js := range perLanguageJobs {
for _, job := range js.CI {
bc.AddJob(job.Name)
w.Jobs[job.Name] = job
}
}
return w
}