-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
267 lines (256 loc) · 7.18 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"fmt"
"os"
"github.com/open-integration/core"
"github.com/open-integration/core/pkg/state"
"github.com/open-integration/core/pkg/task"
)
const (
// PipelineName name of the pipeline to be used by core.engine
PipelineName = "core-ci"
// CreatePVCTask uniqe name for the create-pvc task
CreatePVCTask = "create-pvc"
)
type (
workflowcontext struct {
kube kubeCredentials
pvc string
logsdir string
}
kubeCredentials struct {
host string
crt string
token string
namespace string
}
)
func main() {
kube, err := buildKubeCredentials()
dieOnError(err)
wfcontext := &workflowcontext{
kube: *kube,
}
updateWorkflowContextWithPVC(wfcontext)
updateWorkflowContextWithLogDirectory(wfcontext)
pipe := core.Pipeline{
Metadata: core.PipelineMetadata{
Name: PipelineName,
},
Spec: core.PipelineSpec{
Services: []core.Service{
core.Service{
As: "kubernetes",
Name: "kubernetes",
Version: "0.8.0",
},
},
Reactions: []core.EventReaction{
core.EventReaction{
Condition: core.ConditionEngineStarted(),
Reaction: reactToEngineStartedEvent(wfcontext),
},
core.EventReaction{
Condition: core.ConditionTaskFinished(CreatePVCTask),
Reaction: func(ev state.Event, state state.State) []task.Task {
return []task.Task{
buildKubeRunTask(&kubeRunTaskOptions{
name: "clone",
commands: []string{
"rm -rf core",
"git clone https://github.com/open-integration/core",
},
wfcontext: wfcontext,
}),
}
},
},
core.EventReaction{
Condition: core.ConditionTaskFinishedWithStatus("clone", state.TaskStatusSuccess),
Reaction: func(ev state.Event, state state.State) []task.Task {
return []task.Task{
buildKubeRunTask(
&kubeRunTaskOptions{
name: "download-binaries",
commands: []string{
"cd core",
"go mod tidy",
},
environ: []string{
"GOCACHE=/openintegration/gocache",
"GO111MODULE=on",
},
wfcontext: wfcontext,
}),
}
},
},
core.EventReaction{
Condition: core.ConditionTaskFinishedWithStatus("download-binaries", state.TaskStatusSuccess),
Reaction: func(ev state.Event, state state.State) []task.Task {
return []task.Task{
buildKubeRunTask(
&kubeRunTaskOptions{
name: "test",
commands: []string{
"cd core",
"mkdir .cover",
"make test",
},
environ: []string{
"GOCACHE=/openintegration/gocache",
"GO111MODULE=on",
},
wfcontext: wfcontext,
}),
buildKubeRunTask(
&kubeRunTaskOptions{
name: "test-fmt",
commands: []string{
"cd core",
"make test-fmt",
},
wfcontext: wfcontext,
}),
buildKubeRunTask(&kubeRunTaskOptions{
name: "export-vars",
commands: []string{
"cd core",
"make export-vars",
},
wfcontext: wfcontext,
}),
}
},
},
core.EventReaction{
Condition: core.ConditionTaskFinishedWithStatus("test", state.TaskStatusSuccess),
Reaction: func(ev state.Event, state state.State) []task.Task {
if true || os.Getenv("CODECOV_TOKEN") == "" {
return []task.Task{}
}
envs := []string{
fmt.Sprintf("CODECOV_TOKEN=%s", os.Getenv("CODECOV_TOKEN")),
}
if os.Getenv("CI_BUILD_ID") != "" {
envs = append(envs, fmt.Sprintf("CI_BUILD_ID=%s", os.Getenv("CI_BUILD_ID")))
}
if os.Getenv("CI_BUILD_URL") != "" {
envs = append(envs, fmt.Sprintf("CI_BUILD_URL=%s", os.Getenv("CI_BUILD_URL")))
}
return []task.Task{
buildKubeRunTask(
&kubeRunTaskOptions{
name: "codecov",
commands: []string{
"cd core",
"curl -o codecov.sh https://codecov.io/bash && chmod +x codecov.sh",
"export VCS_BRANCH_NAME=$(cat ./branch.var)",
"export VCS_SLUG=$(cat ./slug.var)",
"export VCS_COMMIT_ID=$(cat ./commit.var)",
"./codecov.sh -e VCS_COMMIT_ID,VCS_BRANCH_NAME,VCS_SLUG,CI_BUILD_ID,CI_BUILD_URL",
},
environ: envs,
wfcontext: wfcontext,
}),
}
},
},
core.EventReaction{
Condition: core.ConditionTaskFinishedWithStatus("export-vars", state.TaskStatusSuccess),
Reaction: func(ev state.Event, state state.State) []task.Task {
if os.Getenv("SNYK_TOKEN") == "" {
return []task.Task{}
}
envs := []string{
fmt.Sprintf("SNYK_TOKEN=%s", os.Getenv("SNYK_TOKEN")),
}
return []task.Task{
buildKubeRunTask(&kubeRunTaskOptions{
name: "security-test",
commands: []string{
"cd core",
"snyk auth $SNYK_TOKEN",
"snyk monitor",
},
environ: envs,
wfcontext: wfcontext,
}),
}
},
},
core.EventReaction{
Condition: core.ConditionCombined(
core.ConditionTaskFinishedWithStatus("security-test", state.TaskStatusSuccess),
core.ConditionTaskFinishedWithStatus("test-fmt", state.TaskStatusSuccess),
core.ConditionTaskFinishedWithStatus("test", state.TaskStatusSuccess),
core.ConditionTaskFinishedWithStatus("codecov", state.TaskStatusSuccess),
),
Reaction: func(ev state.Event, state state.State) []task.Task {
envs := []string{}
if os.Getenv("GITHUB_TOKEN") != "" {
envs = append(envs, os.Getenv("GITHUB_TOKEN"))
}
return []task.Task{
buildKubeRunTask(&kubeRunTaskOptions{
name: "new-release",
commands: []string{
"cd code",
"export VERSION=$(cat ./version.var)",
"git remote rm origin",
"git remote add origin https://olegsu:[email protected]/open-integration/core.git",
"git tag v$VERSION",
"git push --tags",
},
environ: envs,
image: "codefresh/cli",
wfcontext: wfcontext,
}),
}
},
},
},
},
}
opt := &core.EngineOptions{
Pipeline: pipe,
}
if os.Getenv("IN_CLUSTER") != "" {
opt.Kubeconfig = &core.EngineKubernetesOptions{
B64Crt: wfcontext.kube.crt,
Host: wfcontext.kube.host,
Token: wfcontext.kube.token,
InCluster: true,
LogsVolumeClaimName: wfcontext.pvc,
LogsVolumeName: wfcontext.pvc,
Namespace: kube.namespace,
}
}
if wfcontext.logsdir != "" {
opt.LogsDirectory = wfcontext.logsdir
}
e := core.NewEngine(opt)
core.HandleEngineError(e.Run())
}
func reactToEngineStartedEvent(wfcontext *workflowcontext) func(ev state.Event, state state.State) []task.Task {
return func(ev state.Event, state state.State) []task.Task {
return []task.Task{
task.Task{
Metadata: task.Metadata{
Name: CreatePVCTask,
},
Spec: task.Spec{
Service: "kubernetes",
Endpoint: "createpvc",
Arguments: []task.Argument{
buildAuthTaskArgument(wfcontext),
task.Argument{
Key: "PVC",
Value: buildPvcString(wfcontext.kube.namespace, wfcontext.pvc),
},
},
},
},
}
}
}