Skip to content

Commit

Permalink
feat(deploy): log deployments to MS Teams
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Sep 3, 2019
1 parent 1df9145 commit 326161b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions bin/mastarm-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,52 @@ logger
.log(
`:tada: :confetti_ball: :tada: *deploy ${tag} complete* :tada: :confetti_ball: :tada:`
)
.then(() => logToMsTeams())
.then(() => process.exit(0))
)
.catch(err =>
logger
.log(
`:rotating_light: *${tag} error deploying ${tag} ${err.message || err}*`
)
.then(() => logToMsTeams(err))
.then(() => process.exit(1))
)
)

/**
* Sends a card to MS Teams with information about the deployment
* @param {[Error]} error the error, if one occurred. A falsy value indicates
* success
*/
function logToMsTeams (error) {
if (!config.env.MS_TEAMS_WEBHOOK) return Promise.resolve()

const potentialAction = [{
'@type': 'OpenUri',
name: `View Commit on Github`,
targets: [
{
os: 'default',
uri: `${url}/commit/${commit}`
}
]
}]
const text = `📄 *commit:* ${pkg.name}@${commit.slice(0, 6)}\n
👤 *deployed by:* ${username.sync()}\n
🚦 *mastarm:* v${mastarmVersion}\n
☁️ *cloudfront:* ${cloudfront}\n
🌱 *env:* ${env}\n
🗜️ *minify:* ${minify}\n
📦 *s3bucket:* ${s3bucket}\n
${error
? `🚨 🚨 *error deploying ${error.message || error}*`
: `🎉 🎊 🎉 *deploy successful!* 🎉 🎊 🎉`}`

return logger.notifyMsTeams({
potentialAction,
text,
title: `${error ? 'Failed to deploy' : 'Successfully deployed'} ${pkg.name}`,
webhook: config.env.MS_TEAMS_WEBHOOK
})
}
34 changes: 34 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,37 @@ function notifySlack ({ channel, text, webhook }) {
return err
})
}

/**
* Send a message to MS Teams using a webhook. This will generate a card
* according to the following schema:
* https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#openuri-action
*
* @param {Array} potentialAction An array of potential actions on a card.
* @param {String} text The card body text
* @param {String} [themeColor='0072C6'] Card theme color
* @param {String} title Title of the card
* @param {String} webhook Webhook url
*/
module.exports.notifyMsTeams = function notifyMSTeams ({
potentialAction,
text,
themeColor = '0072C6',
title,
webhook
}) {
return fetch(
webhook,
{
method: 'POST',
body: JSON.stringify({
'@context': 'https://schema.org/extensions',
'@type': 'MessageCard',
themeColor,
title,
text,
potentialAction
})
}
)
}

0 comments on commit 326161b

Please sign in to comment.