deploy-notify-slack 0.1.9
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 +116 -0
- package/notify.js +151 -0
- package/package.json +21 -0
- package/tag-deploy.sh +5 -0
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
## Dummy script to send notification about new version deployment to a Slack channel
|
|
2
|
+
|
|
3
|
+
- no npm dependencies, plain nodejs
|
|
4
|
+
- use Slack incoming webhooks API to send a message
|
|
5
|
+
- can attach version description Markdown files
|
|
6
|
+
|
|
7
|
+
### ENV Variables
|
|
8
|
+
|
|
9
|
+
- SLACK_WEBHOOK_URL - you should generate webhook url for your target channel, see: https://api.slack.com/messaging/webhooks
|
|
10
|
+
- STAGE - name of an application stage you're deploying, usually: dev, staging, prod..
|
|
11
|
+
- VERSION - deployed version
|
|
12
|
+
- TITLE - notification title
|
|
13
|
+
- CHANGELOG_PATH - path of your deployed version details file `changelog` by default.
|
|
14
|
+
|
|
15
|
+
> version details file is a Markdown file having the name like `${STAGE}-v${VERSION}.md`.
|
|
16
|
+
> If such file isn't found details block will be omitted in Slack message.
|
|
17
|
+
|
|
18
|
+
### How it works
|
|
19
|
+
|
|
20
|
+
- Generate Slack webhook URL https://api.slack.com/messaging/webhooks
|
|
21
|
+
|
|
22
|
+
- In Bitbucket pipeline or another place you wish to notify about just deployed version of your application you can add dev dependency
|
|
23
|
+
```shell
|
|
24
|
+
npm i --no-save git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:latest
|
|
25
|
+
```
|
|
26
|
+
or major version
|
|
27
|
+
```shell
|
|
28
|
+
npm i --no-save git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:^0.1
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- run the scrypt with your env variables:
|
|
32
|
+
```shell
|
|
33
|
+
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXXXXX STAGE=dev VERSION=1.0.0 node ./node_modules/deploy-notify-slack/notify
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Bitbucket pipeline example:
|
|
37
|
+
```yaml
|
|
38
|
+
- step:
|
|
39
|
+
name: Notify deploy
|
|
40
|
+
image: node:16-alpine
|
|
41
|
+
script:
|
|
42
|
+
- apk add --no-cache bash git openssh
|
|
43
|
+
- npm i --no-save git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:^0.1
|
|
44
|
+
- VERSION=$(npm run version --silent)
|
|
45
|
+
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node ./node_modules/deploy-notify-slack/notify
|
|
46
|
+
```
|
|
47
|
+
or install package globally
|
|
48
|
+
|
|
49
|
+
```yaml
|
|
50
|
+
- step:
|
|
51
|
+
name: Notify Slack
|
|
52
|
+
image: node:16-alpine
|
|
53
|
+
script:
|
|
54
|
+
- apk add --no-cache bash git openssh
|
|
55
|
+
- npm i --location=global git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:^0.1
|
|
56
|
+
- VERSION=$(npm run version --silent)
|
|
57
|
+
- PWD=$(pwd)
|
|
58
|
+
- 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
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Full bitbucket CI/CD pipeline example for deploy NestJs application and send deploy message:
|
|
62
|
+
```yaml
|
|
63
|
+
image: atlassian/default-image:2
|
|
64
|
+
clone:
|
|
65
|
+
depth: full
|
|
66
|
+
pipelines:
|
|
67
|
+
default:
|
|
68
|
+
- step:
|
|
69
|
+
name: Test and Build
|
|
70
|
+
image: node:16-alpine
|
|
71
|
+
caches:
|
|
72
|
+
- node
|
|
73
|
+
script:
|
|
74
|
+
- npm ci
|
|
75
|
+
- npm run test:ci
|
|
76
|
+
- npm run build
|
|
77
|
+
services:
|
|
78
|
+
- database
|
|
79
|
+
artifacts:
|
|
80
|
+
- node_modules/**
|
|
81
|
+
- dist/**
|
|
82
|
+
- step:
|
|
83
|
+
name: Pack and deploy to bundle
|
|
84
|
+
script:
|
|
85
|
+
- VERSION=$(npm run version --silent)
|
|
86
|
+
- cp .env.static .env
|
|
87
|
+
- zip -r application.zip . -x "src/*" -x "docker/*" -x "test/*" -x "cloudformation/*"
|
|
88
|
+
- pipe: atlassian/aws-elasticbeanstalk-deploy:1.0.2
|
|
89
|
+
variables:
|
|
90
|
+
AWS_ACCESS_KEY_ID: $AWS_DEV_ACCESS_KEY_ID
|
|
91
|
+
AWS_SECRET_ACCESS_KEY: $AWS_DEV_SECRET_ACCESS_KEY
|
|
92
|
+
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
|
|
93
|
+
S3_BUCKET: $AWS_DEV_DEPLOY_BUCKET
|
|
94
|
+
VERSION_LABEL: "DEV-${VERSION}-${BITBUCKET_BUILD_NUMBER}-${BITBUCKET_COMMIT:0:8}"
|
|
95
|
+
APPLICATION_NAME: $AWS_DEV_APP_NAME
|
|
96
|
+
ENVIRONMENT_NAME: $AWS_DEV_EB_ENV_NAME
|
|
97
|
+
ZIP_FILE: "application.zip"
|
|
98
|
+
- step:
|
|
99
|
+
name: Notify Slack
|
|
100
|
+
image: node:16-alpine
|
|
101
|
+
script:
|
|
102
|
+
- apk add --no-cache bash git openssh
|
|
103
|
+
- npm i --location=global git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:^0.1
|
|
104
|
+
- VERSION=$(npm run version --silent)
|
|
105
|
+
- SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
106
|
+
|
|
107
|
+
definitions:
|
|
108
|
+
services:
|
|
109
|
+
database:
|
|
110
|
+
image: postgres
|
|
111
|
+
user: postgres
|
|
112
|
+
variables:
|
|
113
|
+
POSTGRES_DB: test
|
|
114
|
+
POSTGRES_USER: api
|
|
115
|
+
POSTGRES_PASSWORD: example
|
|
116
|
+
```
|
package/notify.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
const https = require('https');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path')
|
|
4
|
+
|
|
5
|
+
const slackWebHookURL = process.env.SLACK_WEBHOOK_URL;
|
|
6
|
+
const stage = process.env.STAGE;
|
|
7
|
+
const version = process.env.VERSION;
|
|
8
|
+
const title = process.env.TITLE || 'Deployment';
|
|
9
|
+
const changelogPath = process.env.CHANGELOG_PATH || path.join(__dirname, '../../changelog');
|
|
10
|
+
|
|
11
|
+
function getChangelog() {
|
|
12
|
+
try {
|
|
13
|
+
return fs.readFileSync(path.join(changelogPath, `${stage}-v${version}.md`), 'utf8')
|
|
14
|
+
} catch (e) {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function notificationBody() {
|
|
20
|
+
let blocks = [
|
|
21
|
+
{
|
|
22
|
+
"type": "section",
|
|
23
|
+
"text": {
|
|
24
|
+
"type": "mrkdwn",
|
|
25
|
+
"text": `:rocket: *${title}*`
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"type": "section",
|
|
30
|
+
"fields": [
|
|
31
|
+
{
|
|
32
|
+
"type": "mrkdwn",
|
|
33
|
+
"text": `*stage*\n ${stage}`
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"type": "mrkdwn",
|
|
37
|
+
"text": `*version*\n ${version}`
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const changelog = getChangelog();
|
|
44
|
+
if (changelog) {
|
|
45
|
+
blocks.push(
|
|
46
|
+
{
|
|
47
|
+
"type": "divider"
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
blocks.push({
|
|
51
|
+
"type": "section",
|
|
52
|
+
"text": {
|
|
53
|
+
"type": "mrkdwn",
|
|
54
|
+
"text": "*Description:* \n\n" + changelog
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
"attachments": [
|
|
60
|
+
{
|
|
61
|
+
"blocks": blocks
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Handles the actual sending request.
|
|
69
|
+
* We're turning the https.request into a promise here for convenience
|
|
70
|
+
* @param webhookURL
|
|
71
|
+
* @param messageBody
|
|
72
|
+
* @return {Promise}
|
|
73
|
+
*/
|
|
74
|
+
function sendSlackMessage (webhookURL, messageBody) {
|
|
75
|
+
// make sure the incoming message body can be parsed into valid JSON
|
|
76
|
+
try {
|
|
77
|
+
messageBody = JSON.stringify(messageBody);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
throw new Error('Failed to stringify messageBody', e);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Promisify the https.request
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
// general request options, we defined that it's a POST request and content is JSON
|
|
85
|
+
const requestOptions = {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
header: {
|
|
88
|
+
'Content-Type': 'application/json'
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// actual request
|
|
93
|
+
const req = https.request(webhookURL, requestOptions, (res) => {
|
|
94
|
+
let response = '';
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
res.on('data', (d) => {
|
|
98
|
+
response += d;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// response finished, resolve the promise with data
|
|
102
|
+
res.on('end', () => {
|
|
103
|
+
resolve(response);
|
|
104
|
+
})
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// there was an error, reject the promise
|
|
108
|
+
req.on('error', (e) => {
|
|
109
|
+
reject(e);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// send our message body (was parsed to JSON beforehand)
|
|
113
|
+
req.write(messageBody);
|
|
114
|
+
req.end();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function validate() {
|
|
119
|
+
let success = true;
|
|
120
|
+
if (!slackWebHookURL) {
|
|
121
|
+
console.error('Please fill in slack Webhook URL as SLACK_WEBHOOK_URL env');
|
|
122
|
+
success = false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!stage) {
|
|
126
|
+
console.error('Please fill in deployed stage as STAGE env');
|
|
127
|
+
success = false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!version) {
|
|
131
|
+
console.error('Please fill in deployed version as VERSION env');
|
|
132
|
+
success = false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return success;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// main
|
|
139
|
+
(async function () {
|
|
140
|
+
if (!validate()) {
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
console.log('Sending slack message');
|
|
145
|
+
try {
|
|
146
|
+
const slackResponse = await sendSlackMessage(slackWebHookURL, notificationBody());
|
|
147
|
+
console.log('Message response', slackResponse);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
console.error('There was a error with the request', e);
|
|
150
|
+
}
|
|
151
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deploy-notify-slack",
|
|
3
|
+
"version": "0.1.9",
|
|
4
|
+
"description": "Send Slack notification about deploy with version comments",
|
|
5
|
+
"main": "notify.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"version": "echo $npm_package_version",
|
|
9
|
+
"notify": "node notify.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+ssh://git@bitbucket.org/omvmike/deploy-notify-slack.git"
|
|
14
|
+
},
|
|
15
|
+
"author": "mike.onofrienko@clockwise.software",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://bitbucket.org/omvmike/deploy-notify-slack/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://bitbucket.org/omvmike/deploy-notify-slack#readme"
|
|
21
|
+
}
|