internal/ci/ci.go
141 lines · 5002 bytes
1// Package ci parses .gitbay/ci.yml, the per-repo build configuration:
2//
3// jobs:
4// test:
5// steps:
6// - go test ./...
7//
8// Each job becomes one build per push; each step is a shell command the
9// runner executes with `sh -c`, stopping at the first failure.
10package ci
11
12import (
13 "fmt"
14 "path"
15 "regexp"
16 "sort"
17
18 yaml "go.yaml.in/yaml/v3"
19)
20
21// ConfigPath is where the build configuration lives in a repository.
22const ConfigPath = ".gitbay/ci.yml"
23
24// SecurityJob is the instance's code scanning job, defined by the server's
25// [security] configuration rather than the repository's ci.yml. The name is
26// reserved so a repository's own job cannot be mistaken for it.
27const SecurityJob = "gitbay-security"
28
29const (
30 maxJobs = 10
31 maxSteps = 50
32 maxStepSize = 4096
33 maxPaths = 50
34)
35
36var jobName = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,39}$`)
37
38// imageRef matches an OCI image reference conservatively: registry path
39// segments, an optional :tag and an optional @sha256: digest. This string
40// becomes an argument to `podman run`, and a repository's config file must
41// not be able to turn it into anything else — so the pattern allows only
42// what a reference needs and refuses whitespace and every shell character
43// rather than trying to escape them (#144).
44var imageRef = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._\-]*(:[0-9]+)?(/[a-zA-Z0-9][a-zA-Z0-9._\-]*)*(:[a-zA-Z0-9][a-zA-Z0-9._\-]{0,127})?(@sha256:[a-f0-9]{64})?$`)
45
46type Job struct {
47 Name string
48 Steps []string
49 Schedule string // cron expression; scheduled jobs run on schedule, not on push
50 Tags string // tag glob (e.g. "v*"); tag jobs run on matching tag pushes only
51 // Paths and PathsIgnore only gate a job queued on push; a scheduled
52 // or tag job ignores them.
53 Paths []string // globs; the job runs only when a changed file matches one
54 PathsIgnore []string // globs; the job is skipped when every changed file matches one
55 // Image is the container image the job's steps run in. Empty means
56 // the runner's configured default (#144).
57 Image string
58}
59
60// Parse returns the jobs in name order, or an error describing the first
61// problem so the pusher can fix the file.
62func Parse(raw []byte) ([]Job, error) {
63 var doc struct {
64 Jobs map[string]struct {
65 Steps []string `yaml:"steps"`
66 Schedule string `yaml:"schedule"`
67 Tags string `yaml:"tags"`
68 Paths []string `yaml:"paths"`
69 PathsIgnore []string `yaml:"paths-ignore"`
70 Image string `yaml:"image"`
71 } `yaml:"jobs"`
72 }
73 if err := yaml.Unmarshal(raw, &doc); err != nil {
74 return nil, fmt.Errorf("parsing %s: %w", ConfigPath, err)
75 }
76 if len(doc.Jobs) == 0 {
77 return nil, fmt.Errorf("%s defines no jobs", ConfigPath)
78 }
79 if len(doc.Jobs) > maxJobs {
80 return nil, fmt.Errorf("%s defines %d jobs; max %d", ConfigPath, len(doc.Jobs), maxJobs)
81 }
82 var jobs []Job
83 for name, j := range doc.Jobs {
84 if !jobName.MatchString(name) {
85 return nil, fmt.Errorf("bad job name %q: lowercase letters, digits, - and _; max 40 chars", name)
86 }
87 if name == SecurityJob {
88 return nil, fmt.Errorf("job name %q is reserved for the instance's code scan", name)
89 }
90 if len(j.Steps) == 0 {
91 return nil, fmt.Errorf("job %q has no steps", name)
92 }
93 if len(j.Steps) > maxSteps {
94 return nil, fmt.Errorf("job %q has %d steps; max %d", name, len(j.Steps), maxSteps)
95 }
96 for _, s := range j.Steps {
97 if len(s) > maxStepSize {
98 return nil, fmt.Errorf("job %q has a step over %d bytes", name, maxStepSize)
99 }
100 }
101 if j.Schedule != "" {
102 if _, err := ParseCron(j.Schedule); err != nil {
103 return nil, fmt.Errorf("job %q: %v", name, err)
104 }
105 }
106 if j.Tags != "" {
107 if _, err := path.Match(j.Tags, "x"); err != nil {
108 return nil, fmt.Errorf("job %q: bad tag pattern %q", name, j.Tags)
109 }
110 if j.Schedule != "" {
111 return nil, fmt.Errorf("job %q: schedule and tags are mutually exclusive", name)
112 }
113 }
114 if len(j.Paths) > maxPaths {
115 return nil, fmt.Errorf("job %q has %d path patterns; max %d", name, len(j.Paths), maxPaths)
116 }
117 for _, p := range j.Paths {
118 if _, err := path.Match(p, "x"); err != nil {
119 return nil, fmt.Errorf("job %q: bad path pattern %q", name, p)
120 }
121 }
122 if len(j.PathsIgnore) > maxPaths {
123 return nil, fmt.Errorf("job %q has %d paths-ignore patterns; max %d", name, len(j.PathsIgnore), maxPaths)
124 }
125 for _, p := range j.PathsIgnore {
126 if _, err := path.Match(p, "x"); err != nil {
127 return nil, fmt.Errorf("job %q: bad paths-ignore pattern %q", name, p)
128 }
129 }
130 if j.Image != "" && !imageRef.MatchString(j.Image) {
131 return nil, fmt.Errorf("job %q: bad image %q: a reference like "+
132 "docker.io/library/alpine:3.20, not a command line", name, j.Image)
133 }
134 jobs = append(jobs, Job{
135 Name: name, Steps: j.Steps, Schedule: j.Schedule, Tags: j.Tags,
136 Paths: j.Paths, PathsIgnore: j.PathsIgnore, Image: j.Image,
137 })
138 }
139 sort.Slice(jobs, func(i, k int) bool { return jobs[i].Name < jobs[k].Name })
140 return jobs, nil
141}