This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
forked from Azure/blackbelt-aks-hackfest
-
Notifications
You must be signed in to change notification settings - Fork 35
/
brigade.js
73 lines (63 loc) · 2.73 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
const { events, Job, Group } = require('brigadier')
events.on("push", (brigadeEvent, project) => {
// setup variables
var gitPayload = JSON.parse(brigadeEvent.payload)
var brigConfig = new Map()
brigConfig.set("acrServer", project.secrets.acrServer)
brigConfig.set("acrUsername", project.secrets.acrUsername)
brigConfig.set("acrPassword", project.secrets.acrPassword)
brigConfig.set("webImage", "azureworkshop/rating-web")
brigConfig.set("gitSHA", brigadeEvent.revision.commit.substr(0,7))
brigConfig.set("eventType", brigadeEvent.type)
brigConfig.set("branch", getBranch(gitPayload))
brigConfig.set("imageTag", `${brigConfig.get("branch")}-${brigConfig.get("gitSHA")}`)
brigConfig.set("webACRImage", `${brigConfig.get("acrServer")}/${brigConfig.get("webImage")}`)
console.log(`==> gitHub webook (${brigConfig.get("branch")}) with commit ID ${brigConfig.get("gitSHA")}`)
// setup brigade jobs
var docker = new Job("job-runner-docker")
var k8s = new Job("job-runner-k8s")
dockerJobRunner(brigConfig, docker)
kubeJobRunner(brigConfig, k8s)
// start pipeline
console.log(`==> starting pipeline for docker image: ${brigConfig.get("webACRImage")}:${brigConfig.get("imageTag")}`)
var pipeline = new Group()
pipeline.add(docker)
pipeline.add(k8s)
if (brigConfig.get("branch") == "master") {
pipeline.runEach()
} else {
console.log(`==> no jobs to run when not master`)
}
})
events.on("after", (event, proj) => {
console.log("brigade pipeline finished successfully")
})
function dockerJobRunner(config, d) {
d.storage.enabled = false
d.image = "chzbrgr71/dockernd:node"
d.privileged = true
d.tasks = [
"dockerd-entrypoint.sh &",
"echo waiting && sleep 20",
"cd /src/app/web",
`docker login ${config.get("acrServer")} -u ${config.get("acrUsername")} -p ${config.get("acrPassword")}`,
`docker build --build-arg BUILD_DATE='1/1/2017 5:00' --build-arg IMAGE_TAG_REF=${config.get("imageTag")} --build-arg VCS_REF=${config.get("gitSHA")} -t ${config.get("webImage")} .`,
`docker tag ${config.get("webImage")} ${config.get("webACRImage")}:${config.get("imageTag")}`,
`docker push ${config.get("webACRImage")}:${config.get("imageTag")}`,
"killall dockerd"
]
}
function kubeJobRunner (config, k) {
k.storage.enabled = false
k.image = "lachlanevenson/k8s-kubectl:v1.8.2"
k.tasks = [
`kubectl set image deployment/heroes-web-deploy heroes-web-cntnr=<youracrhere>.azurecr.io/azureworkshop/rating-web:${config.get("imageTag")}`
]
}
function getBranch (p) {
if (p.ref) {
return p.ref.substring(11)
} else {
return "PR"
}
}