Skip to content

Commit

Permalink
chore(slack-notification): use node script (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwambach authored Mar 24, 2020
1 parent 50230e3 commit 88d7767
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
18 changes: 12 additions & 6 deletions ci/cloudbuild-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ steps:
'cache-control:no-cache',
'gs://esa-cfs-versions/web/${BRANCH_NAME}/*',
]
- name: 'gcr.io/cloud-builders/curl'
args: [
'--data',
'{"text": "New deployment to [${BRANCH_NAME}] is ready: \"${_HEAD_BRANCH}\" (${_PR_NUMBER})\n Visit: <https://storage.googleapis.com/esa-cfs-versions/web/${BRANCH_NAME}/index.html>\n Commit: <https://github.com/ubilabs/esa-climate-from-space/commit/${COMMIT_SHA}>"}',
'${_SLACK_HOOK_URL}'
]
- name: gcr.io/cloud-builders/git
args: ['clone', 'https://github.com/ubilabs/esa-climate-from-space', 'repo']
- name: gcr.io/cloud-builders/git
dir: 'repo'
args: ['checkout', '${COMMIT_SHA}']
- name: 'node:12'
dir: 'repo'
env:
- 'SLACK_HOOK_URL=${_SLACK_HOOK_URL}'
- 'BRANCH_NAME=${BRANCH_NAME}'
entrypoint: 'node'
args: ["./scripts/slack-deployment-notification.js"]

30 changes: 30 additions & 0 deletions scripts/slack-deployment-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {execSync} = require('child_process');
const https = require('https');

const url = process.env.SLACK_HOOK_URL;
const branchName = process.env.BRANCH_NAME;

const commitMessage = execSync('git log -1 --pretty=%B', {
encoding: 'utf8'
}).trim();

const commitHash = execSync('git rev-parse HEAD', {
encoding: 'utf8'
}).trim();

const data = JSON.stringify({
text: `New deployment to *${branchName}* is ready: *${commitMessage}*\n Visit: <https://storage.googleapis.com/esa-cfs-versions/web/${branchName}/index.html>\n Commit: <https://github.com/ubilabs/esa-climate-from-space/commit/${commitHash}>`
});

// eslint-disable-next-line no-console
console.log('Message Payload:', data);

const req = https.request(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
});
req.write(data);
req.end();

0 comments on commit 88d7767

Please sign in to comment.