-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpipeline.go
153 lines (127 loc) · 4.11 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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/mohae/deepcopy"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Pipeline struct {
Steps []interface{} `yaml:"steps"`
Notify []interface{} `yaml:"notify,omitempty"`
}
func generateProjectSteps(steps []interface{}, step interface{}, projects []Project) []interface{} {
projectSteps := make([]interface{}, 0)
for _, project := range projects {
stepCopy := deepcopy.Copy(step)
stepCopyMap := stepCopy.(map[interface{}]interface{})
if project.checkProjectRules(stepCopyMap) {
// Unique project level label
stepCopyMap["label"] = fmt.Sprintf("%s %s", stepCopyMap["label"], project.Label)
// Set default buildpipe environment variables and
// set project env vars as step env vars
env := stepCopyMap["env"].(map[interface{}]interface{})
env["BUILDPIPE_PROJECT_LABEL"] = project.Label
env["BUILDPIPE_PROJECT_PATH"] = project.getMainPath()
for envVarName, envVarValue := range project.Env {
env[envVarName] = envVarValue
}
// Unique project level key, if present
if val, ok := stepCopyMap["key"]; ok {
stepCopyMap["key"] = fmt.Sprintf("%s:%s", val, project.Label)
}
// If the step includes a depends_on clause, we need to validate whether each dependency
// is a project-scoped step. If so, the dependency has the current project name added
// to it to match the unique key given above.
if val, ok := stepCopyMap["depends_on"]; ok {
// depends_on can be an array or a string
dependencyList, ok := val.([]interface{})
if !ok {
dependencyList = []interface{}{val}
stepCopyMap["depends_on"] = dependencyList
}
for i, dependency := range dependencyList {
depStr := dependency.(string)
if step := findStepByKey(steps, depStr); step != nil {
if isProjectScopeStep(step) {
dependencyList[i] = fmt.Sprintf("%s:%s", depStr, project.Label)
}
}
}
}
projectSteps = append(projectSteps, stepCopy)
}
}
return projectSteps
}
func isProjectScopeStep(step map[interface{}]interface{}) bool {
if env, ok := step["env"].(map[interface{}]interface{}); ok {
if value, ok := env["BUILDPIPE_SCOPE"]; ok {
return value == "project"
}
}
return false
}
func findStepByKey(steps []interface{}, stepKey string) map[interface{}]interface{} {
for _, step := range steps {
// skip wait commands
stepMap, ok := step.(map[interface{}]interface{})
if !ok {
continue
}
// grab key if it has one and check whether it is project scoped
foundStepKey, ok := stepMap["key"]
if ok && stepKey == foundStepKey {
return stepMap
}
}
return nil
}
func generatePipeline(steps []interface{}, notify []interface{}, pipelineEnv map[string]string, projects []Project) *Pipeline {
generatedSteps := make([]interface{}, 0)
for _, step := range steps {
stepMap, ok := step.(map[interface{}]interface{})
if !ok {
generatedSteps = append(generatedSteps, step)
continue
}
env, foundEnv := stepMap["env"].(map[interface{}]interface{})
_, foundBlockStep := stepMap["block"].(string)
_, foundWaitStep := stepMap["wait"]
if !foundBlockStep && !foundWaitStep {
if !foundEnv {
env = make(map[interface{}]interface{})
stepMap["env"] = env
}
for envVarName, envVarValue := range pipelineEnv {
env[envVarName] = envVarValue
}
}
value, ok := env["BUILDPIPE_SCOPE"]
if ok && value == "project" {
projectSteps := generateProjectSteps(steps, step, projects)
generatedSteps = append(generatedSteps, projectSteps...)
} else {
generatedSteps = append(generatedSteps, step)
}
}
return &Pipeline{
Steps: generatedSteps,
Notify: notify,
}
}
func uploadPipeline(pipeline Pipeline) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "buildpipe-")
if err != nil {
log.Fatalf("Cannot create temporary file: %s\n", err)
}
defer os.Remove(tmpFile.Name())
data, err := yaml.Marshal(&pipeline)
fmt.Printf("Pipeline:\n%s", string(data))
err = ioutil.WriteFile(tmpFile.Name(), data, 0644)
if err != nil {
log.Fatalf("Error writing outfile: %s\n", err)
}
execCommand("buildkite-agent", []string{"pipeline", "upload", tmpFile.Name()})
}