deploy-notify-slack 0.2.1 → 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 +34 -4
- 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,17 +7,20 @@ 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
|
-
console.log(`Description "${stage}-v${version}.md" not found`)
|
|
18
|
+
console.log(`Description "${stage}-v${version}.md" not found in "${changelogPath}"`)
|
|
16
19
|
}
|
|
17
20
|
try {
|
|
18
21
|
return fs.readFileSync(path.join(changelogPath, `v${version}.md`), 'utf8')
|
|
19
22
|
} catch (e) {
|
|
20
|
-
console.log(`Description "v${version}.md" not found`)
|
|
23
|
+
console.log(`Description "v${version}.md" not found in "${changelogPath}"`)
|
|
21
24
|
}
|
|
22
25
|
return '';
|
|
23
26
|
}
|
|
@@ -121,6 +124,24 @@ function sendSlackMessage (webhookURL, messageBody) {
|
|
|
121
124
|
});
|
|
122
125
|
}
|
|
123
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
|
+
|
|
124
145
|
function validate() {
|
|
125
146
|
let success = true;
|
|
126
147
|
if (!slackWebHookURL) {
|
|
@@ -144,14 +165,23 @@ function validate() {
|
|
|
144
165
|
// main
|
|
145
166
|
(async function () {
|
|
146
167
|
if (!validate()) {
|
|
147
|
-
process.exit(
|
|
168
|
+
process.exit(3);
|
|
148
169
|
}
|
|
149
170
|
|
|
150
171
|
console.log('Sending slack message');
|
|
151
172
|
try {
|
|
152
173
|
const slackResponse = await sendSlackMessage(slackWebHookURL, notificationBody());
|
|
153
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
|
+
}
|
|
154
181
|
} catch (e) {
|
|
155
|
-
console.error('There was
|
|
182
|
+
console.error('There was an error with the request', e);
|
|
183
|
+
if (failsIfNotSent) {
|
|
184
|
+
process.exit(5);
|
|
185
|
+
}
|
|
156
186
|
}
|
|
157
187
|
})();
|