Issue
Question
I need to verify the status of a worker node in Jenkins. I need to verify that the node is online. Additionally I need send a Microsoft Teams message or an API call when the node is offline. How can I accomplish this in Jenkins?
Solution
Teams Configuration
First you will need to install the Office 365 Connector plugin on Jenkins.
Go into your Teams channel and select Connector from the options menu
Once you are in the Connector screen search for and click Configure on the Jenkins Connector
You will be prompted to supply a name for the connector. Supply a name of your choice and click create. In Step 2 you will see a Webhook URL. Copy that URL then scroll to the bottom and click done
Jenkins Configuration
Now that you have the Teams Webhook URL you can write your Jenkins Pipeline script. Remember, you need to have the Office 365 Connector plugin installed. Create a new Pipeline and use the following Groovy code
node {
def nodeName = "EC2 (Integrations) - JDK11 Worker (i-0622e9de9e7aaa70d)"
def isOnline = Jenkins.instance.nodes.find { it.name == nodeName }?.computer?.online
def webhook = "https://domain.webhook.office.com/webhookb2/e68084ea-a0a7-49dfJenkinsCI/1c8caa98-bbb8-43b8-abbe-eda2b45832bd"
if( !isOnline ) {
office365ConnectorSend message: "${nodeName} is Offline!", status: "FAILURE", webhookUrl: "${webhook}", color: "d00000"
}
}
You will need to change the nodeName and webhook variable to be the name of your node and the webhook URL you copied from teams, respectively. Thanks to @NoamHelmer for suggesting the use of the Jenkins API over the REST API
You may receive the following error
Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance
To resolve this error either run the pipeline outside the Groovy Sandbox or approve that script signature.
Additional Information
You can also retrieve the offline status of your node through the following REST API
https://${JENKINS_URL}/computer/${NODE}/api/json
The response body from a GET request to this endpoint is as follows ( I have slimed the response down )
{
"displayName": "JDK11-EC2-Instance",
"numExecutors": 2,
"offline": false,
"offlineCause": null,
"offlineCauseReason": "",
"oneOffExecutors": [],
"temporarilyOffline": false,
"absoluteRemotePath": "/home/ec2-user"
}
If you wanted to retrieve the data from the rest service you could do so by using the following Groovy script
node {
def nodeName = "EC2 (Integrations) - JDK11 Worker (i-0622e9de9e7aaa70d)"
def webhook = "https://domain.webhook.office.com/webhookb2/e68084ea-a0a7-49dfJenkinsCI/1c8caa98-bbb8-43b8-abbe-eda2b45832bd"
def response = httpRequest authentication: 'jenkins-credential-id', url: "https://${env.JENKINS_URL}/computer/${nodeName}/api/json"
def json = readJSON text: response.content
if( json['offline'] ) {
office365ConnectorSend message: "${nodeName} is Offline!", status: "FAILURE", webhookUrl: "${webhook}", color: "d00000"
}
}
This example uses the Pipeline Utility Steps plugin, the HTTP Request plugin, and the Office 365 Connector plugin
Answered By - Chris Maggiulli