Creating webhooks from the Jenkins GUI is straightforward. So that’s why here I will explain how to create a webhook as a code to send alerts to a Teams channel. Let’s see the solution.
Prerequisites
- Jenkins installed
- sudo privileges
Solution
Install the
Office 365 Connector
in your Jenkins instance. You can do this by navigating to “Manage Jenkins” > “Manage Plugins” > “Available” tab and searching for the plugin.Generate a webhook URL for your Microsoft Teams channel. To do this, navigate to your Teams channel, click on the three dots on the top right corner and select
Connectors
. Then search forIncoming Webhook
and follow the prompts to set it up.- Add the following code snippet to your Jenkinsfile.
pipeline { agent any stages { stage('Build') { steps { // your build steps here } } } post { always { script{ status = currentBuild.currentResult.toString().toLowerCase() color = status == 'success' ? '00ff00' : 'ff0000' message = "The ${env.JOB_NAME} build ${status} with #${env.BUILD_NUMBER}" url = "Webhook URL" office365ConnectorSend message: message, color: color, webhookUrl: url } } } }
- Replace
Webhook URL
with the webhook URL you generated. Save and run your Jenkinsfile. This code sends a message to your Teams channel after every build, indicating the status of the build and the name of the job.
Conclusion
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.