Jenkins 节点离线时发送 Microsoft Teams 消息

Sending Microsoft Teams message when Jenkins node is offline

问题

我需要验证 Jenkins 中工作节点的状态。我需要验证节点是否在线。此外,我需要在节点离线时发送 Microsoft Teams 消息或 API 呼叫。我如何在 Jenkins 中完成此操作?

团队配置

First you will need to install the Office 365 Connector plugin on Jenkins.

进入您的团队频道并从选项菜单select连接器

进入“连接器”屏幕后,搜索并单击 Jenkins 连接器上的“配置”

系统将提示您提供连接器的名称。提供您选择的名称并单击创建。在第 2 步中,您将看到一个 Webhook URL。复制 URL 然后滚动到底部并单击完成

詹金斯配置

现在您已经有了 Teams Webhook URL,您可以编写 Jenkins 管道脚本。请记住,您需要安装 Office 365 Connector 插件。创建一个新的管道并使用以下 Groovy 代码

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

您可能会收到以下错误

Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance

要解决此错误,运行 管道 在 Groovy 沙箱 之外或批准该脚本签名。

附加信息

您还可以通过以下REST获取您节点的离线状态API

https://${JENKINS_URL}/computer/${NODE}/api/json

从 GET 请求到此端点的响应正文如下(我已经精简了响应)

{
    "displayName": "JDK11-EC2-Instance",
    "numExecutors": 2,
    "offline": false,
    "offlineCause": null,
    "offlineCauseReason": "",
    "oneOffExecutors": [],
    "temporarilyOffline": false,
    "absoluteRemotePath": "/home/ec2-user"
}

如果您想从其余服务中检索数据,可以使用以下 Groovy 脚本

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

多个代理

下面是一个显示多个代理的代码修改的示例。

node {
    
    def nodeNames = ["EC2 (Integrations) - JDK11 Worker (i-0622e9de9e7aaa70d)", "EC2 (Integrations) - JDK7 Worker (i-1223i1de9e7eea70d)"]
    def webhook = "https://domain.webhook.office.com/webhookb2/e68084ea-a0a7-49dfJenkinsCI/1c8caa98-bbb8-43b8-abbe-eda2b45832bd"

    nodeNames.each{ nodeName ->
        def isOnline =  Jenkins.instance.nodes.find { it.name == nodeName }?.computer?.online

        if( !isOnline ) {
            office365ConnectorSend message: "${nodeName} is Offline!", status: "FAILURE", webhookUrl: "${webhook}", color: "d00000"
        }
    }

}