forked from jwplayer/buildpipe-buildkite-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (53 loc) · 1.45 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
package main
import (
"io/ioutil"
"os"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const pluginPrefix = "BUILDKITE_PLUGIN_BUILDPIPE_"
type Config struct {
Projects []Project `yaml:"projects"`
Steps []interface{} `yaml:"steps"`
}
func NewConfig(filename string) *Config {
config := Config{}
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %s\n", filename, err)
}
if err = yaml.Unmarshal(yamlFile, &config); err != nil {
log.Fatalf("Error unmarshalling: %s\n", err)
}
return &config
}
func getAffectedProjects(projects []Project, changedFiles []string) []Project {
affectedProjects := make([]Project, 0)
for _, project := range projects {
if project.checkAffected(changedFiles) {
affectedProjects = append(affectedProjects, project)
}
}
return affectedProjects
}
func main() {
logLevel := getEnv(pluginPrefix+"LOG_LEVEL", "info")
ll, err := log.ParseLevel(logLevel)
if err != nil {
ll = log.InfoLevel
}
log.SetLevel(ll)
config := NewConfig(os.Getenv(pluginPrefix + "DYNAMIC_PIPELINE"))
changedFiles := getChangedFiles()
if len(changedFiles) == 0 {
log.Info("No files were changed")
os.Exit(0)
}
affectedProjects := getAffectedProjects(config.Projects, changedFiles)
if len(affectedProjects) == 0 {
log.Info("No project was affected from git changes")
os.Exit(0)
}
pipeline := generatePipeline(config.Steps, affectedProjects)
uploadPipeline(*pipeline)
}