deploy-notify-slack 0.1.12 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -3
- package/notify.js +39 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,14 +6,24 @@
|
|
|
6
6
|
|
|
7
7
|
### ENV Variables
|
|
8
8
|
|
|
9
|
+
#### Required
|
|
10
|
+
|
|
9
11
|
- SLACK_WEBHOOK_URL - you should generate webhook url for your target channel, see: https://api.slack.com/messaging/webhooks
|
|
10
12
|
- STAGE - name of an application stage you're deploying, usually: dev, staging, prod..
|
|
11
13
|
- VERSION - deployed version
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
|
|
15
|
+
#### Optional
|
|
16
|
+
|
|
17
|
+
- TITLE - ('Deployment' by default) notification title
|
|
18
|
+
- CHANGELOG_PATH - path of your deployed version details file (`changelog` by default as well as we assume that the package installed locally, so this option is required if the package installed globally)
|
|
14
19
|
|
|
15
20
|
> version details file is a Markdown file having the name like `${STAGE}-v${VERSION}.md`.
|
|
16
|
-
>
|
|
21
|
+
>
|
|
22
|
+
> you can also create cross-environment file with name pattern `v${VERSION}.md` and if script cannot find stage specific description it will get this one.
|
|
23
|
+
>
|
|
24
|
+
> If no decription file found details block will be omitted in Slack message.
|
|
25
|
+
|
|
26
|
+
- FAILS_IF_NOT_SENT - (false by default) Should exit with not 0 error code if message was not sent successfully.
|
|
17
27
|
|
|
18
28
|
### How it works
|
|
19
29
|
|
package/notify.js
CHANGED
|
@@ -7,13 +7,22 @@ const stage = process.env.STAGE;
|
|
|
7
7
|
const version = process.env.VERSION;
|
|
8
8
|
const title = process.env.TITLE || 'Deployment';
|
|
9
9
|
const changelogPath = process.env.CHANGELOG_PATH || path.join(__dirname, '../../changelog');
|
|
10
|
+
const failsIfNotSent = process.env.FAILS_IF_NOT_SENT !== undefined
|
|
11
|
+
? stringToBool(process.env.FAILS_IF_NOT_SENT, false)
|
|
12
|
+
: false;
|
|
10
13
|
|
|
11
14
|
function getChangelog() {
|
|
12
15
|
try {
|
|
13
16
|
return fs.readFileSync(path.join(changelogPath, `${stage}-v${version}.md`), 'utf8')
|
|
14
17
|
} catch (e) {
|
|
15
|
-
|
|
18
|
+
console.log(`Description "${stage}-v${version}.md" not found in "${changelogPath}"`)
|
|
16
19
|
}
|
|
20
|
+
try {
|
|
21
|
+
return fs.readFileSync(path.join(changelogPath, `v${version}.md`), 'utf8')
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.log(`Description "v${version}.md" not found in "${changelogPath}"`)
|
|
24
|
+
}
|
|
25
|
+
return '';
|
|
17
26
|
}
|
|
18
27
|
|
|
19
28
|
function notificationBody() {
|
|
@@ -115,6 +124,24 @@ function sendSlackMessage (webhookURL, messageBody) {
|
|
|
115
124
|
});
|
|
116
125
|
}
|
|
117
126
|
|
|
127
|
+
function stringToBool(str, defaultValue = false){
|
|
128
|
+
switch(str.toLowerCase().trim()){
|
|
129
|
+
case "true":
|
|
130
|
+
case "yes":
|
|
131
|
+
case "1":
|
|
132
|
+
return true;
|
|
133
|
+
|
|
134
|
+
case "false":
|
|
135
|
+
case "no":
|
|
136
|
+
case "0":
|
|
137
|
+
case null:
|
|
138
|
+
return false;
|
|
139
|
+
|
|
140
|
+
default:
|
|
141
|
+
return defaultValue
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
118
145
|
function validate() {
|
|
119
146
|
let success = true;
|
|
120
147
|
if (!slackWebHookURL) {
|
|
@@ -138,14 +165,23 @@ function validate() {
|
|
|
138
165
|
// main
|
|
139
166
|
(async function () {
|
|
140
167
|
if (!validate()) {
|
|
141
|
-
process.exit(
|
|
168
|
+
process.exit(3);
|
|
142
169
|
}
|
|
143
170
|
|
|
144
171
|
console.log('Sending slack message');
|
|
145
172
|
try {
|
|
146
173
|
const slackResponse = await sendSlackMessage(slackWebHookURL, notificationBody());
|
|
147
174
|
console.log('Message response', slackResponse);
|
|
175
|
+
if (slackResponse !== 'ok') {
|
|
176
|
+
console.error('There was an error with the request. Check SLACK_WEBHOOK_URL.');
|
|
177
|
+
if (failsIfNotSent) {
|
|
178
|
+
process.exit(4);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
148
181
|
} catch (e) {
|
|
149
|
-
console.error('There was
|
|
182
|
+
console.error('There was an error with the request', e);
|
|
183
|
+
if (failsIfNotSent) {
|
|
184
|
+
process.exit(5);
|
|
185
|
+
}
|
|
150
186
|
}
|
|
151
187
|
})();
|