deploy-notify-slack 0.5.9 → 0.6.1
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 +104 -63
- package/notify.js +28 -49
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -1,82 +1,124 @@
|
|
|
1
|
-
|
|
1
|
+
# deploy-notify-slack
|
|
2
2
|
|
|
3
3
|
<a href="https://www.npmjs.com/package/deploy-notify-slack" target="_blank"><img src="https://img.shields.io/npm/v/deploy-notify-slack" alt="NPM Version" /></a>
|
|
4
4
|
<a href="https://www.npmjs.com/package/deploy-notify-slack" target="_blank"><img src="https://img.shields.io/npm/l/deploy-notify-slack" alt="Package License" /></a>
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
- use Slack incoming webhooks API to send a message
|
|
8
|
-
- can attach version description Markdown files
|
|
6
|
+
Send Slack notifications about deployments via incoming webhooks.
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
You can use default message template with the following env variables:
|
|
8
|
+
## Features
|
|
12
9
|
|
|
13
|
-
|
|
10
|
+
- Zero dependencies - uses native Node.js `fetch()` API
|
|
11
|
+
- Automatic changelog attachment from Markdown files
|
|
12
|
+
- Customizable colors, emojis, and titles
|
|
13
|
+
- Full custom message support via Slack Block Kit
|
|
14
|
+
- Simple environment variable configuration
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
- STAGE - name of an application stage you're deploying, usually: dev, staging, prod..
|
|
17
|
-
- VERSION - deployed version
|
|
16
|
+
## Requirements
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
- **Node.js 21+** (uses native `fetch()` API)
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
- 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)
|
|
23
|
-
- COLOR - ('#7f8583' by default) left bar notification color. Should be hex color code without `#` symbol
|
|
24
|
-
- EMOJI - (':rocket:' by default) emoji to be displayed in the notification title
|
|
25
|
-
- MAX_BLOCKS - (5 by default) maximum amount of large blocks(2500 symbols) available in slack message. If your changelog is bigger than this value it will be truncated.
|
|
20
|
+
> Need Node.js 8.x-20.x support? Use [v0.5.10](https://www.npmjs.com/package/deploy-notify-slack/v/0.5.10)
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
>
|
|
29
|
-
> 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.
|
|
30
|
-
>
|
|
31
|
-
> If no description file found details block will be omitted in Slack message.
|
|
22
|
+
## Quick Start
|
|
32
23
|
|
|
33
|
-
|
|
24
|
+
1. Generate a [Slack Webhook URL](https://api.slack.com/messaging/webhooks)
|
|
34
25
|
|
|
35
|
-
|
|
26
|
+
2. Run with npx (no installation required):
|
|
27
|
+
```bash
|
|
28
|
+
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXX \
|
|
29
|
+
STAGE=production \
|
|
30
|
+
VERSION=1.0.0 \
|
|
31
|
+
npx deploy-notify-slack
|
|
32
|
+
```
|
|
36
33
|
|
|
37
|
-
|
|
34
|
+
Or install and run with node:
|
|
35
|
+
```bash
|
|
36
|
+
npm install deploy-notify-slack
|
|
37
|
+
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXX \
|
|
38
|
+
STAGE=production \
|
|
39
|
+
VERSION=1.0.0 \
|
|
40
|
+
node ./node_modules/deploy-notify-slack/notify.js
|
|
41
|
+
```
|
|
38
42
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
**npx (no install required):**
|
|
46
|
+
```bash
|
|
47
|
+
npx deploy-notify-slack
|
|
42
48
|
```
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
|
|
50
|
+
**Local install (recommended for CI/CD):**
|
|
51
|
+
```bash
|
|
52
|
+
npm install --save-dev deploy-notify-slack
|
|
53
|
+
npx deploy-notify-slack
|
|
46
54
|
```
|
|
47
55
|
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
|
|
56
|
+
**Global install:**
|
|
57
|
+
```bash
|
|
58
|
+
npm install --location=global deploy-notify-slack
|
|
59
|
+
deploy-notify-slack
|
|
51
60
|
```
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
## Configuration
|
|
63
|
+
|
|
64
|
+
### Required Environment Variables
|
|
65
|
+
|
|
66
|
+
| Variable | Description |
|
|
67
|
+
|----------|-------------|
|
|
68
|
+
| `SLACK_WEBHOOK_URL` | Webhook URL for your Slack channel ([how to generate](https://api.slack.com/messaging/webhooks)) |
|
|
69
|
+
| `STAGE` | Deployment stage (e.g., `dev`, `staging`, `prod`) |
|
|
70
|
+
| `VERSION` | Version being deployed |
|
|
71
|
+
|
|
72
|
+
### Optional Environment Variables
|
|
73
|
+
|
|
74
|
+
| Variable | Default | Description |
|
|
75
|
+
|----------|---------|-------------|
|
|
76
|
+
| `TITLE` | `Deployment` | Notification title |
|
|
77
|
+
| `COLOR` | `7f8583` | Left bar color (hex without `#`) |
|
|
78
|
+
| `EMOJI` | `:rocket:` | Title emoji |
|
|
79
|
+
| `MAX_BLOCKS` | `5` | Max 2500-char blocks before truncation |
|
|
80
|
+
| `CHANGELOG_PATH` | `./changelog` | Path to changelog directory |
|
|
81
|
+
| `FAILS_IF_NOT_SENT` | `false` | Exit with error if send fails |
|
|
82
|
+
| `CUSTOM_MESSAGE` | - | JSON for custom Slack Block Kit message |
|
|
83
|
+
|
|
84
|
+
### Changelog File Resolution
|
|
85
|
+
|
|
86
|
+
The script looks for changelog files in the following order:
|
|
87
|
+
|
|
88
|
+
1. `{CHANGELOG_PATH}/{STAGE}-v{VERSION}.md` (stage-specific, e.g., `prod-v1.0.0.md`)
|
|
89
|
+
2. `{CHANGELOG_PATH}/v{VERSION}.md` (version-specific, e.g., `v1.0.0.md`)
|
|
90
|
+
3. `{CHANGELOG_PATH}/changelog.md` (fallback)
|
|
91
|
+
|
|
92
|
+
If no changelog file is found, the notification is sent without a changelog attachment.
|
|
93
|
+
|
|
94
|
+
## CI/CD Integration
|
|
95
|
+
|
|
96
|
+
### Bitbucket Pipelines
|
|
97
|
+
|
|
98
|
+
**Using npx (recommended):**
|
|
54
99
|
```yaml
|
|
55
100
|
- step:
|
|
56
|
-
name: Notify
|
|
57
|
-
image: node:
|
|
101
|
+
name: Notify Slack
|
|
102
|
+
image: node:24-alpine
|
|
58
103
|
script:
|
|
59
|
-
- npm i --location=global deploy-notify-slack
|
|
60
104
|
- VERSION=$(npm run version --silent)
|
|
61
|
-
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION
|
|
105
|
+
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION CHANGELOG_PATH=$PWD/changelog npx deploy-notify-slack@^0.6
|
|
62
106
|
```
|
|
63
107
|
|
|
64
|
-
|
|
108
|
+
> The `version` script above is `echo $npm_package_version` in package.json
|
|
65
109
|
|
|
110
|
+
**Local install (caches better in CI):**
|
|
66
111
|
```yaml
|
|
67
112
|
- step:
|
|
68
113
|
name: Notify Slack
|
|
69
|
-
image: node:
|
|
114
|
+
image: node:24-alpine
|
|
70
115
|
script:
|
|
71
|
-
- npm
|
|
116
|
+
- npm install --no-save deploy-notify-slack@^0.6
|
|
72
117
|
- VERSION=$(npm run version --silent)
|
|
73
|
-
- PWD
|
|
74
|
-
- 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
|
|
118
|
+
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION CHANGELOG_PATH=$PWD/changelog npx deploy-notify-slack
|
|
75
119
|
```
|
|
76
|
-
> version script above is just a `echo $npm_package_version` command
|
|
77
|
-
|
|
78
120
|
|
|
79
|
-
Full
|
|
121
|
+
**Full pipeline example (NestJS + AWS Elastic Beanstalk):**
|
|
80
122
|
```yaml
|
|
81
123
|
image: atlassian/default-image:2
|
|
82
124
|
clone:
|
|
@@ -85,7 +127,7 @@ pipelines:
|
|
|
85
127
|
default:
|
|
86
128
|
- step:
|
|
87
129
|
name: Test and Build
|
|
88
|
-
image: node:
|
|
130
|
+
image: node:24-alpine
|
|
89
131
|
caches:
|
|
90
132
|
- node
|
|
91
133
|
script:
|
|
@@ -115,12 +157,11 @@ pipelines:
|
|
|
115
157
|
ZIP_FILE: "application.zip"
|
|
116
158
|
- step:
|
|
117
159
|
name: Notify Slack
|
|
118
|
-
image: node:
|
|
160
|
+
image: node:24-alpine
|
|
119
161
|
script:
|
|
120
|
-
- npm i --location=global deploy-notify-slack
|
|
121
162
|
- VERSION=$(npm run version --silent)
|
|
122
|
-
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION
|
|
123
|
-
|
|
163
|
+
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION CHANGELOG_PATH=$PWD/changelog npx deploy-notify-slack@^0.6
|
|
164
|
+
|
|
124
165
|
definitions:
|
|
125
166
|
services:
|
|
126
167
|
database:
|
|
@@ -129,19 +170,16 @@ definitions:
|
|
|
129
170
|
variables:
|
|
130
171
|
POSTGRES_DB: test
|
|
131
172
|
POSTGRES_USER: api
|
|
132
|
-
POSTGRES_PASSWORD: example
|
|
173
|
+
POSTGRES_PASSWORD: example
|
|
133
174
|
```
|
|
134
175
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
You can specify your own message template instead of default one.
|
|
138
|
-
It's useful if you want to add some additional information to the message.
|
|
176
|
+
## Custom Messages
|
|
139
177
|
|
|
140
|
-
|
|
178
|
+
You can specify your own message template instead of the default one using the `CUSTOM_MESSAGE` environment variable.
|
|
141
179
|
|
|
142
|
-
|
|
180
|
+
Use the [Slack Block Kit Builder](https://api.slack.com/tools/block-kit-builder) to design your message, then pass it as JSON:
|
|
143
181
|
|
|
144
|
-
|
|
182
|
+
**Example message.json:**
|
|
145
183
|
```json
|
|
146
184
|
{
|
|
147
185
|
"blocks": [
|
|
@@ -157,9 +195,12 @@ For example you saved your message template to `message.json` file:
|
|
|
157
195
|
}
|
|
158
196
|
```
|
|
159
197
|
|
|
160
|
-
|
|
161
|
-
```
|
|
162
|
-
npm i --location=global deploy-notify-slack@latest
|
|
198
|
+
**Usage:**
|
|
199
|
+
```bash
|
|
163
200
|
CUSTOM_MESSAGE=$(cat message.json)
|
|
164
|
-
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} CUSTOM_MESSAGE=$CUSTOM_MESSAGE
|
|
201
|
+
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} CUSTOM_MESSAGE=$CUSTOM_MESSAGE npx deploy-notify-slack
|
|
165
202
|
```
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
MIT
|
package/notify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
|
|
@@ -37,9 +37,14 @@ function getChangelog() {
|
|
|
37
37
|
|
|
38
38
|
function notificationBody() {
|
|
39
39
|
if (customMessage) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
try {
|
|
41
|
+
return {
|
|
42
|
+
'attachments': [JSON.parse(customMessage)],
|
|
43
|
+
};
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.error('Failed to parse CUSTOM_MESSAGE as JSON:', e.message);
|
|
46
|
+
process.exit(3);
|
|
47
|
+
}
|
|
43
48
|
}
|
|
44
49
|
let blocks = [
|
|
45
50
|
{
|
|
@@ -102,53 +107,25 @@ function notificationBody() {
|
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
/**
|
|
105
|
-
* Handles the actual sending request.
|
|
106
|
-
* We're turning the https.request into a promise here for convenience
|
|
110
|
+
* Handles the actual sending request using fetch API.
|
|
107
111
|
* @param webhookURL
|
|
108
112
|
* @param messageBody
|
|
109
113
|
* @return {Promise}
|
|
110
114
|
*/
|
|
111
|
-
function sendSlackMessage(webhookURL, messageBody) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// Promisify the https.request
|
|
120
|
-
return new Promise((resolve, reject) => {
|
|
121
|
-
// general request options, we defined that it's a POST request and content is JSON
|
|
122
|
-
const requestOptions = {
|
|
123
|
-
method: 'POST',
|
|
124
|
-
header: {
|
|
125
|
-
'Content-Type': 'application/json',
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
// actual request
|
|
130
|
-
const req = https.request(webhookURL, requestOptions, (res) => {
|
|
131
|
-
let response = '';
|
|
132
|
-
|
|
133
|
-
res.on('data', (d) => {
|
|
134
|
-
response += d;
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// response finished, resolve the promise with data
|
|
138
|
-
res.on('end', () => {
|
|
139
|
-
resolve(response);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
115
|
+
async function sendSlackMessage(webhookURL, messageBody) {
|
|
116
|
+
const response = await fetch(webhookURL, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: {
|
|
119
|
+
'Content-Type': 'application/json',
|
|
120
|
+
},
|
|
121
|
+
body: JSON.stringify(messageBody),
|
|
122
|
+
});
|
|
142
123
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
});
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
|
|
126
|
+
}
|
|
147
127
|
|
|
148
|
-
|
|
149
|
-
req.write(messageBody);
|
|
150
|
-
req.end();
|
|
151
|
-
});
|
|
128
|
+
return response.text();
|
|
152
129
|
}
|
|
153
130
|
|
|
154
131
|
function splitTextToBlocks(text) {
|
|
@@ -205,15 +182,17 @@ function stringToBool(str, defaultValue = false) {
|
|
|
205
182
|
|
|
206
183
|
function validate() {
|
|
207
184
|
let success = true;
|
|
208
|
-
|
|
209
|
-
console.log('Custom message', customMessage);
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
185
|
+
|
|
212
186
|
if (!slackWebHookURL) {
|
|
213
187
|
console.error('Please fill in slack Webhook URL as SLACK_WEBHOOK_URL env');
|
|
214
188
|
success = false;
|
|
215
189
|
}
|
|
216
190
|
|
|
191
|
+
if (customMessage) {
|
|
192
|
+
console.log('Custom message', customMessage);
|
|
193
|
+
return success;
|
|
194
|
+
}
|
|
195
|
+
|
|
217
196
|
if (!stage) {
|
|
218
197
|
console.error('Please fill in deployed stage as STAGE env');
|
|
219
198
|
success = false;
|
package/package.json
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deploy-notify-slack",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Send Slack notification about deploy with version comments",
|
|
6
6
|
"main": "notify.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"deploy-notify-slack": "./notify.js"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=21"
|
|
12
|
+
},
|
|
7
13
|
"scripts": {
|
|
8
14
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
15
|
"version": "echo $npm_package_version",
|