forked from brigadecore/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brigade.js
309 lines (269 loc) · 8.8 KB
/
brigade.js
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ============================================================================
// NOTE: This is the actual brigade.js file for testing the Brigade project.
// Be careful when editing!
// ============================================================================
const { events, Job, Group} = require("brigadier")
const goImg = "golang:1.11"
// TODO: Possible/preferred to use canonical image list in Makefile?
// Would necessitate looping inside of Job, i.e., within one shell task
const images = [
"brig",
"brigade-api",
"brigade-controller",
"brigade-cr-gateway",
"brigade-vacuum",
"brigade-worker", // brigade-worker does not have a rootfs. Could probably minify src into one and save space
"git-sidecar",
"brigade-github-gateway"
]
function build(e, project) {
// This is a Go project, so we want to set it up for Go.
var gopath = "/go"
// To set GOPATH correctly, we have to override the default
// path that Brigade sets.
var localPath = gopath + "/src/github.com/" + project.repo.name;
// Create a new job to run Go tests
var goBuild = new Job("brigade-test", goImg);
// Set a few environment variables.
goBuild.env = {
"DEST_PATH": localPath,
"GOPATH": gopath
};
// Run Go unit tests
goBuild.tasks = [
// Need to move the source into GOPATH so vendor/ works as desired.
"mkdir -p " + localPath,
"mv /src/* " + localPath,
"cd " + localPath,
"make vendor",
"make test-style",
"make test-unit"
];
// Run the brigade worker tests
var jsTest = new Job("brigade-js-build", "node:8");
jsTest.tasks = [
"cd /src/brigade-worker",
"yarn install",
"yarn test"
];
start = ghNotify("pending", `Build started as ${ e.buildID }`, e, project)
// Run tests in parallel. Then if it's a release, push binaries.
// Then send GitHub a notification on the status.
Group.runAll([start, jsTest, goBuild])
.then(() => {
return ghNotify("success", `Build ${ e.buildID } passed`, e, project).run()
}).then( () => {
var runRelease = false
if (e.type == "push") {
// Push to master: run "latest" release, don't release brig
if (e.revision.ref.includes("refs/heads/master")) {
runRelease = true
return goDockerBuild(project, e.revision.ref).run()
.then(() => {
Group.runAll([
acrBuild(project, "latest"),
dockerhubPublish(project, "latest")
])
})
}
// Tag pushed: run tag release, release brig
if (e.revision.ref.startsWith("refs/tags/")) {
runRelease = true
let parts = e.revision.ref.split("/", 3)
let tag = parts[2]
return goDockerBuild(project, tag).run()
.then(() => {
Group.runAll([
acrBuild(project, tag),
dockerhubPublish(project, tag)
])
})
.then(() => {
releaseBrig(project, tag)
})
}
}
return Promise.resolve(runRelease)
}).catch(err => {
return ghNotify("failure", `failed build ${ e.buildID }`, e, project).run()
});
}
function releaseBrig(p, tag) {
if (!p.secrets.ghToken) {
throw new Error("Project must have 'secrets.ghToken' set")
}
const binName = "brig"
const gopath = "/go"
const localPath = gopath + "/src/github.com/" + p.repo.name;
// Cross-compile binaries for a given release and upload them to GitHub.
var cx = new Job("cross-compile", goImg)
cx.storage.enabled = true
parts = p.repo.name.split("/", 2)
cx.env = {
GITHUB_USER: parts[0],
GITHUB_REPO: parts[1],
GITHUB_TOKEN: p.secrets.ghToken,
GOPATH: gopath
}
cx.tasks = [
"go get github.com/aktau/github-release",
`cd /src`,
`git checkout ${tag}`,
// Need to move the source into GOPATH so vendor/ works as desired.
`mkdir -p ${localPath}`,
`cp -a /src/* ${localPath}`,
`cp -a /src/.git ${localPath}`,
`cd ${localPath}`,
"make vendor",
"make build-release",
`github-release release -t ${tag} -n "${parts[1]} ${tag}" || echo "release ${tag} exists"`
];
// Upload for each target that we support
for (const f of ["linux-amd64", "windows-amd64.exe", "darwin-amd64"]) {
var name = binName + "-" + f;
var outname = name;
cx.tasks.push(`github-release upload -f ./bin/${name} -n ${outname} -t ${tag}`)
}
console.log(cx.tasks);
console.log(`releases at https://github.com/${p.repo.name}/releases/tag/${tag}`);
return cx.run();
}
function ghNotify(state, msg, e, project) {
const gh = new Job(`notify-${ state }`, "technosophos/github-notify:latest")
gh.imageForcePull = true;
gh.env = {
GH_REPO: project.repo.name,
GH_STATE: state,
GH_DESCRIPTION: msg,
GH_CONTEXT: "brigade",
GH_TOKEN: project.secrets.ghToken,
GH_COMMIT: e.revision.commit,
GH_TARGET_URL: `https://azure.github.io/kashti/builds/${ e.buildID }`,
}
return gh
}
function goDockerBuild(project, tag) {
// We build in a separate pod b/c AKS's Docker is too old to do multi-stage builds.
const goBuild = new Job("brigade-docker-build", goImg);
const gopath = "/go"
const localPath = gopath + "/src/github.com/" + project.repo.name;
goBuild.storage.enabled = true;
goBuild.env = {
"DEST_PATH": localPath,
"GOPATH": gopath
};
goBuild.tasks = [
`cd /src && git checkout ${tag}`,
`mkdir -p ${localPath}/bin`,
`mv /src/* ${localPath}`,
`cd ${localPath}`,
"make vendor",
"make build-docker-bins"
];
for (let i of images) {
goBuild.tasks.push(
// Copy the Docker rootfs of each binary into shared storage. This is
// a little tricky because worker is non-Go, so later we will have
// to copy them back.
`mkdir -p /mnt/brigade/share/${i}/rootfs`,
// If there's no rootfs, we're done. Otherwise, copy it.
`[ ! -d ${i}/rootfs ] || cp -a ./${i}/rootfs/* /mnt/brigade/share/${i}/rootfs/`,
);
}
goBuild.tasks.push("ls -lah /mnt/brigade/share");
return goBuild;
}
function dockerhubPublish(project, tag) {
const publisher = new Job("dockerhub-publish", "docker");
let dockerRegistry = project.secrets.dockerhubRegistry || "docker.io";
let dockerOrg = project.secrets.dockerhubOrg || "deis";
publisher.docker.enabled = true;
publisher.storage.enabled = true;
publisher.tasks = [
"apk add --update --no-cache make",
`docker login ${dockerRegistry} -u ${project.secrets.dockerhubUsername} -p ${project.secrets.dockerhubPassword}`,
"cd /src"
];
for (let i of images) {
publisher.tasks.push(
`cp -av /mnt/brigade/share/${i}/rootfs ./${i}`,
`DOCKER_REGISTRY=${dockerOrg} VERSION=${tag} make ${i}-image ${i}-push`
);
}
publisher.tasks.push(`docker logout ${dockerRegistry}`);
return publisher;
}
function acrBuild(project, tag) {
const acrImagePrefix = "public/deis/"
let registry = project.secrets.acrRegistry || "brigade"
var builder = new Job("az-build", "microsoft/azure-cli:latest")
builder.imageForcePull = true;
builder.storage.enabled = true;
builder.tasks = [
// Create a service principal and assign it proper perms on the ACR.
`az login --service-principal -u ${project.secrets.acrName} -p '${project.secrets.acrToken}' --tenant ${project.secrets.acrTenant}`,
`cd /src`,
`mkdir -p ./bin`
]
// For each image, build, tag and finally push to registry
for (let i of images) {
let imgName = acrImagePrefix+i;
let tagged = imgName+":"+tag;
builder.tasks.push(
`cd ${i}`,
`echo '========> Building ${i}'`,
`cp -av /mnt/brigade/share/${i}/rootfs ./`,
`az acr build -r ${registry} -t ${tagged} .`,
`echo '<======== Finished ${i}'`,
`cd ..`
);
}
return builder;
}
events.on("push", build)
events.on("pull_request", build)
events.on("release_brig", (e, p) => {
/*
* Expects JSON of the form {'tag': 'v1.2.3'}
*/
payload = JSON.parse(e.payload)
if (!payload.tag) {
throw error("No tag specified")
}
releaseBrig(p, payload.tag)
})
events.on("release_images", (e, p) => {
/*
* Expects JSON of the form {'tag': 'v1.2.3'}
*/
payload = JSON.parse(e.payload)
if (!payload.tag) {
throw error("No tag specified")
}
goDockerBuild(p, payload.tag).run()
.then(() => {
Group.runAll([
acrBuild(p, payload.tag),
dockerhubPublish(p, payload.tag)
])
});
})
events.on("image_push", (e, p) => {
console.log(e.payload)
var m = "New image pushed"
if (project.secrets.SLACK_WEBHOOK) {
var slack = new Job("slack-notify")
slack.image = "technosophos/slack-notify:latest"
slack.env = {
SLACK_WEBHOOK: project.secrets.SLACK_WEBHOOK,
SLACK_USERNAME: "BrigadeBot",
SLACK_TITLE: "DockerHub Image",
SLACK_MESSAGE: m + " <https://" + project.repo.name + ">",
SLACK_COLOR: "#00ff00"
}
slack.tasks = ["/slack-notify"]
slack.run()
} else {
console.log(m)
}
})