deploy-notify-slack 0.3.1 → 0.4.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/LICENSE +21 -0
- package/README.md +44 -11
- package/notify.js +10 -1
- package/package.json +1 -1
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,18 +1,19 @@
|
|
|
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
|
+
### Use default message template
|
|
8
|
+
You can use default message template with the following env variables:
|
|
8
9
|
|
|
9
|
-
#### Required
|
|
10
|
+
#### Required env variables
|
|
10
11
|
|
|
11
12
|
- SLACK_WEBHOOK_URL - you should generate webhook url for your target channel, see: https://api.slack.com/messaging/webhooks
|
|
12
13
|
- STAGE - name of an application stage you're deploying, usually: dev, staging, prod..
|
|
13
14
|
- VERSION - deployed version
|
|
14
15
|
|
|
15
|
-
#### Optional
|
|
16
|
+
#### Optional env variables
|
|
16
17
|
|
|
17
18
|
- TITLE - ('Deployment' by default) notification title
|
|
18
19
|
- 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)
|
|
@@ -21,21 +22,21 @@
|
|
|
21
22
|
>
|
|
22
23
|
> 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
|
>
|
|
24
|
-
> If no
|
|
25
|
+
> If no description file found details block will be omitted in Slack message.
|
|
25
26
|
|
|
26
27
|
- FAILS_IF_NOT_SENT - (false by default) Should exit with not 0 error code if message was not sent successfully.
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
#### How it works
|
|
29
30
|
|
|
30
31
|
- Generate Slack webhook URL https://api.slack.com/messaging/webhooks
|
|
31
32
|
|
|
32
33
|
- In Bitbucket pipeline or another place you wish to notify about just deployed version of your application you can add dev dependency
|
|
33
34
|
```shell
|
|
34
|
-
npm i --no-save deploy-notify-slack@^0.
|
|
35
|
+
npm i --no-save deploy-notify-slack@^0.3
|
|
35
36
|
```
|
|
36
37
|
or major version
|
|
37
38
|
```shell
|
|
38
|
-
npm i --location=global deploy-notify-slack@^0.
|
|
39
|
+
npm i --location=global deploy-notify-slack@^0.3
|
|
39
40
|
```
|
|
40
41
|
|
|
41
42
|
- run the scrypt with your env variables:
|
|
@@ -49,7 +50,7 @@ Bitbucket pipeline example:
|
|
|
49
50
|
name: Notify deploy
|
|
50
51
|
image: node:16-alpine
|
|
51
52
|
script:
|
|
52
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
53
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
53
54
|
- VERSION=$(npm run version --silent)
|
|
54
55
|
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node ./node_modules/deploy-notify-slack/notify
|
|
55
56
|
```
|
|
@@ -61,7 +62,7 @@ or install package globally
|
|
|
61
62
|
name: Notify Slack
|
|
62
63
|
image: node:16-alpine
|
|
63
64
|
script:
|
|
64
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
65
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
65
66
|
- VERSION=$(npm run version --silent)
|
|
66
67
|
- PWD=$(pwd)
|
|
67
68
|
- 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
|
|
@@ -110,7 +111,7 @@ pipelines:
|
|
|
110
111
|
name: Notify Slack
|
|
111
112
|
image: node:16-alpine
|
|
112
113
|
script:
|
|
113
|
-
- npm i --location=global deploy-notify-slack@^0.
|
|
114
|
+
- npm i --location=global deploy-notify-slack@^0.3
|
|
114
115
|
- VERSION=$(npm run version --silent)
|
|
115
116
|
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
116
117
|
|
|
@@ -124,3 +125,35 @@ definitions:
|
|
|
124
125
|
POSTGRES_USER: api
|
|
125
126
|
POSTGRES_PASSWORD: example
|
|
126
127
|
```
|
|
128
|
+
|
|
129
|
+
### Use custom message template
|
|
130
|
+
|
|
131
|
+
You can specify your own message template instead of default one.
|
|
132
|
+
It's useful if you want to add some additional information to the message.
|
|
133
|
+
|
|
134
|
+
Try to use [Slack message builder](https://api.slack.com/tools/block-kit-builder) to create your own message template.
|
|
135
|
+
|
|
136
|
+
Then you should load your template from file and pass it to the script as env variable `CUSTOM_MESSAGE`:
|
|
137
|
+
|
|
138
|
+
For example you saved your message template to `message.json` file:
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"blocks": [
|
|
142
|
+
{
|
|
143
|
+
"type": "header",
|
|
144
|
+
"text": {
|
|
145
|
+
"type": "plain_text",
|
|
146
|
+
"text": ":flying_saucer: New API deploy of stage *DEV*",
|
|
147
|
+
"emoji": true
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Then you can run the script with the following command:
|
|
155
|
+
```shell
|
|
156
|
+
npm i --location=global deploy-notify-slack@latest
|
|
157
|
+
CUSTOM_MESSAGE=$(cat message.json)
|
|
158
|
+
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} CUSTOM_MESSAGE=$CUSTOM_MESSAGE node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
159
|
+
```
|
package/notify.js
CHANGED
|
@@ -6,6 +6,7 @@ const slackWebHookURL = process.env.SLACK_WEBHOOK_URL;
|
|
|
6
6
|
const stage = process.env.STAGE;
|
|
7
7
|
const version = process.env.VERSION;
|
|
8
8
|
const title = process.env.TITLE || 'Deployment';
|
|
9
|
+
const customMessage = process.env.CUSTOM_MESSAGE;
|
|
9
10
|
const changelogPath = process.env.CHANGELOG_PATH || path.join(__dirname, '../../changelog');
|
|
10
11
|
const failsIfNotSent = process.env.FAILS_IF_NOT_SENT !== undefined
|
|
11
12
|
? stringToBool(process.env.FAILS_IF_NOT_SENT, false)
|
|
@@ -26,6 +27,11 @@ function getChangelog() {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
function notificationBody() {
|
|
30
|
+
if (customMessage) {
|
|
31
|
+
return {
|
|
32
|
+
"attachments": [ JSON.parse(customMessage) ]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
29
35
|
let blocks = [
|
|
30
36
|
{
|
|
31
37
|
"type": "section",
|
|
@@ -144,6 +150,10 @@ function stringToBool(str, defaultValue = false){
|
|
|
144
150
|
|
|
145
151
|
function validate() {
|
|
146
152
|
let success = true;
|
|
153
|
+
if (customMessage) {
|
|
154
|
+
console.log('Custom message', customMessage);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
147
157
|
if (!slackWebHookURL) {
|
|
148
158
|
console.error('Please fill in slack Webhook URL as SLACK_WEBHOOK_URL env');
|
|
149
159
|
success = false;
|
|
@@ -167,7 +177,6 @@ function validate() {
|
|
|
167
177
|
if (!validate()) {
|
|
168
178
|
process.exit(3);
|
|
169
179
|
}
|
|
170
|
-
|
|
171
180
|
console.log('Sending slack message');
|
|
172
181
|
try {
|
|
173
182
|
const slackResponse = await sendSlackMessage(slackWebHookURL, notificationBody());
|