Skip to content

Commit bb2eae2

Browse files
author
Christian Weichel
committed
[build] Send slack notification when build fails
1 parent bef78d4 commit bb2eae2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

.werft/build.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,63 @@ const { werft, exec, gitTag } = require('./util/shell.js');
44
const { sleep } = require('./util/util.js');
55
const { wipeAndRecreateNamespace, setKubectlContextNamespace, deleteNonNamespaceObjects } = require('./util/kubectl.js');
66
const { issueAndInstallCertficate } = require('./util/certs.js');
7+
const https = require('https');
78

89
const GCLOUD_SERVICE_ACCOUNT_PATH = "/mnt/secrets/gcp-sa/service-account.json";
910

1011
const context = JSON.parse(fs.readFileSync('context.json'));
1112

1213
const version = parseVersion(context);
1314
build(context, version)
14-
.catch((err) => process.exit(1));
15+
.catch((err) => {
16+
if (context.Repository.ref === "refs/heads/master") {
17+
const repo = context.Repository.host + "/" + context.Repository.owner + "/" + context.Repository.repo;
18+
const data = JSON.stringify({
19+
"blocks": [
20+
{
21+
"type": "section",
22+
"text": {
23+
"type": "mrkdwn",
24+
"text": ":X: *build failure*\n_Repo:_ `" + repo + "`\n_Build:_ `" + context.Name + "`"
25+
},
26+
"accessory": {
27+
"type": "button",
28+
"text": {
29+
"type": "plain_text",
30+
"text": "Go to Werft",
31+
"emoji": true
32+
},
33+
"value": "click_me_123",
34+
"url": "https://werft.gitpod-dev.com/job/" + context.Name,
35+
"action_id": "button-action"
36+
}
37+
},
38+
{
39+
"type": "section",
40+
"text": {
41+
"type": "mrkdwn",
42+
"text": "```\n" + err + "\n```"
43+
}
44+
}
45+
]
46+
});
47+
const req = https.request({
48+
hostname: "hooks.slack.com",
49+
port: 443,
50+
path: process.env.SLACK_NOTIFICATION_PATH.trim(),
51+
method: "POST",
52+
headers: {
53+
'Content-Type': 'application/json',
54+
'Content-Length': data.length,
55+
}
56+
}, () => process.exit(1));
57+
req.on('error', () => process.exit(1));
58+
req.write(data);
59+
req.end();
60+
} else {
61+
process.exit(1);
62+
}
63+
});
1564

1665
function parseVersion(context) {
1766
let buildConfig = context.Annotations || {};

.werft/build.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ pod:
6868
secretKeyRef:
6969
name: npm-auth-token
7070
key: npm-auth-token.json
71+
- name: SLACK_NOTIFICATION_PATH
72+
valueFrom:
73+
secretKeyRef:
74+
name: slack-path
75+
key: slackPath
7176
# - name: GITPOD_TEST_TOKEN_GITHUB
7277
# valueFrom:
7378
# secretKeyRef:

0 commit comments

Comments
 (0)