deploy-notify-slack 0.2.0 → 0.3.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 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
- - TITLE - notification title
13
- - CHANGELOG_PATH - path of your deployed version details file `changelog` by default.
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
- > If such file isn't found details block will be omitted in Slack message.
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
 
@@ -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 git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:latest
34
+ npm i --no-save deploy-notify-slack@^0.1
25
35
  ```
26
36
  or major version
27
37
  ```shell
28
- npm i --no-save git@bitbucket.org:omvmike/deploy-notify-slack.git#semver:^0.1
38
+ npm i --location=global deploy-notify-slack@^0.1
29
39
  ```
30
40
 
31
41
  - run the scrypt with your env variables:
@@ -39,11 +49,11 @@ Bitbucket pipeline example:
39
49
  name: Notify deploy
40
50
  image: node:16-alpine
41
51
  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
52
+ - npm i --location=global deploy-notify-slack@^0.1
44
53
  - VERSION=$(npm run version --silent)
45
54
  - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} STAGE=dev VERSION=$VERSION node ./node_modules/deploy-notify-slack/notify
46
55
  ```
56
+
47
57
  or install package globally
48
58
 
49
59
  ```yaml
@@ -51,12 +61,13 @@ or install package globally
51
61
  name: Notify Slack
52
62
  image: node:16-alpine
53
63
  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
64
+ - npm i --location=global deploy-notify-slack@^0.1
56
65
  - VERSION=$(npm run version --silent)
57
66
  - PWD=$(pwd)
58
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
59
68
  ```
69
+ > version script above is just a `echo $npm_package_version` command
70
+
60
71
 
61
72
  Full bitbucket CI/CD pipeline example for deploy NestJs application and send deploy message:
62
73
  ```yaml
@@ -99,8 +110,7 @@ pipelines:
99
110
  name: Notify Slack
100
111
  image: node:16-alpine
101
112
  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
113
+ - npm i --location=global deploy-notify-slack@^0.1
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,13 +7,22 @@ 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
- return '';
18
+ console.log(`Description "${stage}-v${version}.md" not found in "${changelogPath}"`)
16
19
  }
20
+ try {
21
+ return fs.readFileSync(path.join(changelogPath, `v${version}.md`), 'utf8')
22
+ } catch (e) {
23
+ console.log(`Description "v${version}.md" not found in "${changelogPath}"`)
24
+ }
25
+ return '';
17
26
  }
18
27
 
19
28
  function notificationBody() {
@@ -115,6 +124,24 @@ function sendSlackMessage (webhookURL, messageBody) {
115
124
  });
116
125
  }
117
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
+
118
145
  function validate() {
119
146
  let success = true;
120
147
  if (!slackWebHookURL) {
@@ -138,14 +165,23 @@ function validate() {
138
165
  // main
139
166
  (async function () {
140
167
  if (!validate()) {
141
- process.exit(1);
168
+ process.exit(3);
142
169
  }
143
170
 
144
171
  console.log('Sending slack message');
145
172
  try {
146
173
  const slackResponse = await sendSlackMessage(slackWebHookURL, notificationBody());
147
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
+ }
148
181
  } catch (e) {
149
- console.error('There was a error with the request', e);
182
+ console.error('There was an error with the request', e);
183
+ if (failsIfNotSent) {
184
+ process.exit(5);
185
+ }
150
186
  }
151
187
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deploy-notify-slack",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Send Slack notification about deploy with version comments",
5
5
  "main": "notify.js",
6
6
  "scripts": {
@@ -1,18 +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
- - npm version minor -m "Upgrade to %s [skip ci]"
15
- - git push && git push --tags
16
- - pipe: atlassian/npm-publish:0.2.0
17
- variables:
18
- NPM_TOKEN: $NPM_TOKEN
package/tag-deploy.sh DELETED
@@ -1,5 +0,0 @@
1
- #!/bin/bash
2
-
3
- VERSION=$(npm run version --silent)
4
- git tag -a $VERSION -m "new version"
5
- git push origin $VERSION