internal/ci/ci.go
145 lines · 5167 bytes
11 symbols in this file
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// ValidJobName reports whether name could name a job: one of ci.yml's, or
39// SecurityJob.
40func ValidJobName(name string) bool { return jobName.MatchString(name) }
41
42// imageRef matches an OCI image reference conservatively: registry path
43// segments, an optional :tag and an optional @sha256: digest. This string
44// becomes an argument to `podman run`, and a repository's config file must
45// not be able to turn it into anything else — so the pattern allows only
46// what a reference needs and refuses whitespace and every shell character
47// rather than trying to escape them (#144).
48var 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})?$`)
49
50type Job struct {
51 Name string
52 Steps []string
53 Schedule string // cron expression; scheduled jobs run on schedule, not on push
54 Tags string // tag glob (e.g. "v*"); tag jobs run on matching tag pushes only
55 // Paths and PathsIgnore only gate a job queued on push; a scheduled
56 // or tag job ignores them.
57 Paths []string // globs; the job runs only when a changed file matches one
58 PathsIgnore []string // globs; the job is skipped when every changed file matches one
59 // Image is the container image the job's steps run in. Empty means
60 // the runner's configured default (#144).
61 Image string
62}
63
64// Parse returns the jobs in name order, or an error describing the first
65// problem so the pusher can fix the file.
66func Parse(raw []byte) ([]Job, error) {
67 var doc struct {
68 Jobs map[string]struct {
69 Steps []string `yaml:"steps"`
70 Schedule string `yaml:"schedule"`
71 Tags string `yaml:"tags"`
72 Paths []string `yaml:"paths"`
73 PathsIgnore []string `yaml:"paths-ignore"`
74 Image string `yaml:"image"`
75 } `yaml:"jobs"`
76 }
77 if err := yaml.Unmarshal(raw, &doc); err != nil {
78 return nil, fmt.Errorf("parsing %s: %w", ConfigPath, err)
79 }
80 if len(doc.Jobs) == 0 {
81 return nil, fmt.Errorf("%s defines no jobs", ConfigPath)
82 }
83 if len(doc.Jobs) > maxJobs {
84 return nil, fmt.Errorf("%s defines %d jobs; max %d", ConfigPath, len(doc.Jobs), maxJobs)
85 }
86 var jobs []Job
87 for name, j := range doc.Jobs {
88 if !jobName.MatchString(name) {
89 return nil, fmt.Errorf("bad job name %q: lowercase letters, digits, - and _; max 40 chars", name)
90 }
91 if name == SecurityJob {
92 return nil, fmt.Errorf("job name %q is reserved for the instance's code scan", name)
93 }
94 if len(j.Steps) == 0 {
95 return nil, fmt.Errorf("job %q has no steps", name)
96 }
97 if len(j.Steps) > maxSteps {
98 return nil, fmt.Errorf("job %q has %d steps; max %d", name, len(j.Steps), maxSteps)
99 }
100 for _, s := range j.Steps {
101 if len(s) > maxStepSize {
102 return nil, fmt.Errorf("job %q has a step over %d bytes", name, maxStepSize)
103 }
104 }
105 if j.Schedule != "" {
106 if _, err := ParseCron(j.Schedule); err != nil {
107 return nil, fmt.Errorf("job %q: %v", name, err)
108 }
109 }
110 if j.Tags != "" {
111 if _, err := path.Match(j.Tags, "x"); err != nil {
112 return nil, fmt.Errorf("job %q: bad tag pattern %q", name, j.Tags)
113 }
114 if j.Schedule != "" {
115 return nil, fmt.Errorf("job %q: schedule and tags are mutually exclusive", name)
116 }
117 }
118 if len(j.Paths) > maxPaths {
119 return nil, fmt.Errorf("job %q has %d path patterns; max %d", name, len(j.Paths), maxPaths)
120 }
121 for _, p := range j.Paths {
122 if _, err := path.Match(p, "x"); err != nil {
123 return nil, fmt.Errorf("job %q: bad path pattern %q", name, p)
124 }
125 }
126 if len(j.PathsIgnore) > maxPaths {
127 return nil, fmt.Errorf("job %q has %d paths-ignore patterns; max %d", name, len(j.PathsIgnore), maxPaths)
128 }
129 for _, p := range j.PathsIgnore {
130 if _, err := path.Match(p, "x"); err != nil {
131 return nil, fmt.Errorf("job %q: bad paths-ignore pattern %q", name, p)
132 }
133 }
134 if j.Image != "" && !imageRef.MatchString(j.Image) {
135 return nil, fmt.Errorf("job %q: bad image %q: a reference like "+
136 "docker.io/library/alpine:3.20, not a command line", name, j.Image)
137 }
138 jobs = append(jobs, Job{
139 Name: name, Steps: j.Steps, Schedule: j.Schedule, Tags: j.Tags,
140 Paths: j.Paths, PathsIgnore: j.PathsIgnore, Image: j.Image,
141 })
142 }
143 sort.Slice(jobs, func(i, k int) bool { return jobs[i].Name < jobs[k].Name })
144 return jobs, nil
145}