This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
pipeline.go
207 lines (166 loc) · 4.4 KB
/
pipeline.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
package main
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"github.com/bmatcuk/doublestar/v2"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// WaitStep represents a Buildkite Wait Step
// https://buildkite.com/docs/pipelines/wait-step
// We can't use Step here since the value for Wait is always nil
// regardless of whether or not we want to include the key.
type WaitStep struct{}
func (WaitStep) MarshalYAML() (interface{}, error) {
return map[string]interface{}{
"wait": nil,
}, nil
}
func (s Step) MarshalYAML() (interface{}, error) {
if s.Group == "" {
type Alias Step
return (Alias)(s), nil
}
label := s.Group
s.Group = ""
return Group{Label: label, Steps: []Step{s}}, nil
}
func (n PluginNotify) MarshalYAML() (interface{}, error) {
return n, nil
}
// PipelineGenerator generates pipeline file
type PipelineGenerator func(steps []Step, plugin Plugin) (*os.File, error)
func uploadPipeline(plugin Plugin, generatePipeline PipelineGenerator) (string, []string, error) {
diffOutput, err := diff(plugin.Diff)
if err != nil {
log.Fatal(err)
return "", []string{}, err
}
if len(diffOutput) < 1 {
log.Info("No changes detected. Skipping pipeline upload.")
return "", []string{}, nil
}
log.Debug("Output from diff: \n" + strings.Join(diffOutput, "\n"))
steps, err := stepsToTrigger(diffOutput, plugin.Watch)
if err != nil {
return "", []string{}, err
}
pipeline, err := generatePipeline(steps, plugin)
defer os.Remove(pipeline.Name())
if err != nil {
log.Error(err)
return "", []string{}, err
}
cmd := "buildkite-agent"
args := []string{"pipeline", "upload", pipeline.Name()}
if !plugin.Interpolation {
args = append(args, "--no-interpolation")
}
_, err = executeCommand("buildkite-agent", args)
return cmd, args, err
}
func diff(command string) ([]string, error) {
log.Infof("Running diff command: %s", command)
output, err := executeCommand(
env("SHELL", "bash"),
[]string{"-c", strings.Replace(command, "\n", " ", -1)},
)
if err != nil {
return nil, fmt.Errorf("diff command failed: %v", err)
}
return strings.Fields(strings.TrimSpace(output)), nil
}
func stepsToTrigger(files []string, watch []WatchConfig) ([]Step, error) {
steps := []Step{}
for _, w := range watch {
for _, p := range w.Paths {
for _, f := range files {
match, err := matchPath(p, f)
if err != nil {
return nil, err
}
if match {
steps = append(steps, w.Step)
break
}
}
}
}
return dedupSteps(steps), nil
}
// matchPath checks if the file f matches the path p.
func matchPath(p string, f string) (bool, error) {
// If the path contains a glob, the `doublestar.Match`
// method is used to determine the match,
// otherwise `strings.HasPrefix` is used.
if strings.Contains(p, "*") {
match, err := doublestar.Match(p, f)
if err != nil {
return false, fmt.Errorf("path matching failed: %v", err)
}
if match {
return true, nil
}
}
if strings.HasPrefix(f, p) {
return true, nil
}
return false, nil
}
func dedupSteps(steps []Step) []Step {
unique := []Step{}
for _, p := range steps {
duplicate := false
for _, t := range unique {
if reflect.DeepEqual(p, t) {
duplicate = true
break
}
}
if !duplicate {
unique = append(unique, p)
}
}
return unique
}
func generatePipeline(steps []Step, plugin Plugin) (*os.File, error) {
tmp, err := ioutil.TempFile(os.TempDir(), "bmrd-")
if err != nil {
return nil, fmt.Errorf("could not create temporary pipeline file: %v", err)
}
yamlSteps := make([]yaml.Marshaler, len(steps))
for i, step := range steps {
yamlSteps[i] = step
}
if plugin.Wait {
yamlSteps = append(yamlSteps, WaitStep{})
}
for _, cmd := range plugin.Hooks {
yamlSteps = append(yamlSteps, Step{Command: cmd.Command})
}
yamlNotify := make([]yaml.Marshaler, len(plugin.Notify))
for i, n := range plugin.Notify {
yamlNotify[i] = n
}
pipeline := map[string][]yaml.Marshaler{
"steps": yamlSteps,
}
if len(yamlNotify) > 0 {
pipeline["notify"] = yamlNotify
}
data, err := yaml.Marshal(&pipeline)
if err != nil {
return nil, fmt.Errorf("could not serialize the pipeline: %v", err)
}
// Disable logging in context of go tests.
if env("TEST_MODE", "") != "true" {
fmt.Printf("Generated Pipeline:\n%s\n", string(data))
}
if err = ioutil.WriteFile(tmp.Name(), data, 0644); err != nil {
return nil, fmt.Errorf("could not write step to temporary file: %v", err)
}
return tmp, nil
}