deploy-notify-slack 0.2.1 → 0.3.2
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/LICENSE +21 -0
- package/README.md +19 -9
- package/notify.js +34 -4
- package/package.json +1 -1
- package/bitbucket-pipelines.yml +0 -16
- package/tag-deploy.sh +0 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Mike
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
## Dummy script to send notification about new version deployment to a Slack channel
|
|
2
2
|
|
|
3
|
-
- no npm dependencies, plain nodejs
|
|
3
|
+
- no npm dependencies, plain nodejs 8.x or higher
|
|
4
4
|
- use Slack incoming webhooks API to send a message
|
|
5
5
|
- can attach version description Markdown files
|
|
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 description 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
|
|
|
@@ -21,11 +31,11 @@
|
|
|
21
31
|
|
|
22
32
|
- In Bitbucket pipeline or another place you wish to notify about just deployed version of your application you can add dev dependency
|
|
23
33
|
```shell
|
|
24
|
-
npm i --no-save deploy-notify-slack@^0.
|
|
34
|
+
npm i --no-save deploy-notify-slack@^0.3
|
|
25
35
|
```
|
|
26
36
|
or major version
|
|
27
37
|
```shell
|
|
28
|
-
npm i --location=global deploy-notify-slack@^0.
|
|
38
|
+
npm i --location=global deploy-notify-slack@^0.3
|
|
29
39
|
```
|
|
30
40
|
|
|
31
41
|
- run the scrypt with your env variables:
|
|
@@ -39,7 +49,7 @@ Bitbucket pipeline example:
|
|
|
39
49
|
name: Notify deploy
|
|
40
50
|
image: node:16-alpine
|
|
41
51
|
script:
|
|
42
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
52
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
43
53
|
- VERSION=$(npm run version --silent)
|
|
44
54
|
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node ./node_modules/deploy-notify-slack/notify
|
|
45
55
|
```
|
|
@@ -51,7 +61,7 @@ or install package globally
|
|
|
51
61
|
name: Notify Slack
|
|
52
62
|
image: node:16-alpine
|
|
53
63
|
script:
|
|
54
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
64
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
55
65
|
- VERSION=$(npm run version --silent)
|
|
56
66
|
- PWD=$(pwd)
|
|
57
67
|
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION CHANGELOG_PATH=$PWD/changelog node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
@@ -100,7 +110,7 @@ pipelines:
|
|
|
100
110
|
name: Notify Slack
|
|
101
111
|
image: node:16-alpine
|
|
102
112
|
script:
|
|
103
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
113
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
104
114
|
- VERSION=$(npm run version --silent)
|
|
105
115
|
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
106
116
|
|
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
|
})();
|
package/package.json
CHANGED
package/bitbucket-pipelines.yml
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
image: node:16-alpine
|
|
2
|
-
|
|
3
|
-
pipelines:
|
|
4
|
-
default:
|
|
5
|
-
# - step:
|
|
6
|
-
# name: Build and Test
|
|
7
|
-
# script:
|
|
8
|
-
# - npm install
|
|
9
|
-
# - npm test
|
|
10
|
-
- step:
|
|
11
|
-
name: Publish
|
|
12
|
-
deployment: production
|
|
13
|
-
script:
|
|
14
|
-
- pipe: atlassian/npm-publish:0.2.0
|
|
15
|
-
variables:
|
|
16
|
-
NPM_TOKEN: $NPM_TOKEN
|