UNPKG

jenkins-msteams-notification

Version:

Simple webhook wrapper that uses Jenkins build environment variables to send build failure notifications to channels on Microsoft Teams

83 lines (72 loc) 2.31 kB
#! /usr/bin/env node const https = require('https'); const {URL} = require('url'); if (!process.env.MSTEAMS_WEBHOOK_URL) { console.error('Please provide a webhook URL by using setting the MSTEAMS_WEBHOOK_URL environment variable'); process.exit(1); } const actions = [ { "type": "Action.OpenUrl", "title": "View Build", "url": process.env.BUILD_URL } ]; if (process.env.GIT_URL && process.env.GIT_COMMIT) { actions.push({ "type": "Action.OpenUrl", "title": "View Commit", "url": `${process.env.GIT_URL.replace('.git', '')}/commit/${process.env.GIT_COMMIT}`, }); } const data = { "type": "message", "attachments": [ { "contentType": "application/vnd.microsoft.card.adaptive", "contentUrl": null, "content": { "$schema": "https://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.2", "body": [ { "type": "TextBlock", "text": `${decodeURIComponent(process.env.JOB_BASE_NAME)} build failed`, "color": "attention", "size": "large" }, { "type": "TextBlock", "wrap": true, "text": `The ${decodeURIComponent(process.env.JOB_NAME)} build ${process.env.BUILD_DISPLAY_NAME} has failed.` }, { "type": "ActionSet", "actions": actions, } ] } } ] }; const serialisedData = JSON.stringify(data); const url = new URL(process.env.MSTEAMS_WEBHOOK_URL); const options = { hostname: url.hostname, port: 443, path: url.pathname, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': serialisedData.length } }; const req = https.request(options, (res) => { console.log('Notification sent to MS Teams. Response code:', res.statusCode); }); req.on('error', (e) => { console.error('Unable to notify MS Teams', e); }); req.write(serialisedData); req.end();