检查 Jenkins 节点是否在线,否则发送电子邮件警报
Check if Jenkins node is online for the job, otherwise send email alert
将 Jenkins 作业专用于特殊节点,如果该作业无法 运行 因为节点处于离线状态,我希望收到通知。是否可以设置此功能?
换句话说,如果在节点离线('pending'作业状态)时作业已经启动,则默认的Jenkins行为是等待该节点。在这种情况下,我想失败(或根本不开始)作业并发送 'node offline' 邮件。
这个节点检查内容应该在作业中,因为作业很少执行,而且我不在乎作业不需要节点时节点是否离线。我已经尝试过外部节点监视插件,但它并没有完全按照我的要求进行操作 - 每次节点离线时它都会触发电子邮件并且在我的情况下它是多余的。
我不认为检查节点是否可用可以在您想要 运行 的作业中完成(例如 JobX
)。检查行为,特别是在执行时针对您的 JobX
,本身需要一项工作才能 运行 - 我不知道 plugin/configuration 选项可以做到这一点。 JobX
无法检查节点是否空闲 JobX
。
我使用了很多流程作业(正在转换为管道逻辑),其中 JobA
将触发 JobB
,因此 JobA
可以 运行 在 master 上在您的情况下检查 JobB
、JobX
的节点,如果启动则触发它。
JobA
需要自由式作业,运行 需要 'execute system groovy script > Groovy command' 构建步骤。下面的 groovy 代码是从许多工作示例中提取出来的,因此未经测试:
import hudson.model.*;
import hudson.AbortException;
import java.util.concurrent.CancellationException;
def allNodes = jenkins.model.Jenkins.instance.nodes
def triggerJob = false
for (node in allNodes) {
if ( node.getComputer().isOnline() && node.nodeName == "special_node" ) {
println node.nodeName + " " + node.getComputer().countBusy() + " " + node.getComputer().getOneOffExecutors().size
triggerJob = true
break
}
}
if (triggerJob) {
println("triggering child build as node available")
def job = Hudson.instance.getJob('JobB')
def anotherBuild
try {
def params = [
new StringParameterValue('ParamOne', '123'),
]
def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
anotherBuild = future.get()
} catch (CancellationException x) {
throw new AbortException("${job.fullDisplayName} aborted.")
}
} else {
println("failing parent build as node not available")
build.getExecutor().interrupt(hudson.model.Result.FAILURE)
throw new InterruptedException()
}
要获取节点离线电子邮件,您可以触发 post 构建操作以在失败时发送电子邮件。
我找到了答案 here。
您可以添加调用 curl 命令并处理结果
的命令行或 PowerShell 块
curl --silent $JENKINS_URL/computer/$JENKINS_NODENAME/api/json
结果 json 包含 离线 属性 和 true/false 值
将 Jenkins 作业专用于特殊节点,如果该作业无法 运行 因为节点处于离线状态,我希望收到通知。是否可以设置此功能?
换句话说,如果在节点离线('pending'作业状态)时作业已经启动,则默认的Jenkins行为是等待该节点。在这种情况下,我想失败(或根本不开始)作业并发送 'node offline' 邮件。
这个节点检查内容应该在作业中,因为作业很少执行,而且我不在乎作业不需要节点时节点是否离线。我已经尝试过外部节点监视插件,但它并没有完全按照我的要求进行操作 - 每次节点离线时它都会触发电子邮件并且在我的情况下它是多余的。
我不认为检查节点是否可用可以在您想要 运行 的作业中完成(例如 JobX
)。检查行为,特别是在执行时针对您的 JobX
,本身需要一项工作才能 运行 - 我不知道 plugin/configuration 选项可以做到这一点。 JobX
无法检查节点是否空闲 JobX
。
我使用了很多流程作业(正在转换为管道逻辑),其中 JobA
将触发 JobB
,因此 JobA
可以 运行 在 master 上在您的情况下检查 JobB
、JobX
的节点,如果启动则触发它。
JobA
需要自由式作业,运行 需要 'execute system groovy script > Groovy command' 构建步骤。下面的 groovy 代码是从许多工作示例中提取出来的,因此未经测试:
import hudson.model.*;
import hudson.AbortException;
import java.util.concurrent.CancellationException;
def allNodes = jenkins.model.Jenkins.instance.nodes
def triggerJob = false
for (node in allNodes) {
if ( node.getComputer().isOnline() && node.nodeName == "special_node" ) {
println node.nodeName + " " + node.getComputer().countBusy() + " " + node.getComputer().getOneOffExecutors().size
triggerJob = true
break
}
}
if (triggerJob) {
println("triggering child build as node available")
def job = Hudson.instance.getJob('JobB')
def anotherBuild
try {
def params = [
new StringParameterValue('ParamOne', '123'),
]
def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
anotherBuild = future.get()
} catch (CancellationException x) {
throw new AbortException("${job.fullDisplayName} aborted.")
}
} else {
println("failing parent build as node not available")
build.getExecutor().interrupt(hudson.model.Result.FAILURE)
throw new InterruptedException()
}
要获取节点离线电子邮件,您可以触发 post 构建操作以在失败时发送电子邮件。
我找到了答案 here。 您可以添加调用 curl 命令并处理结果
的命令行或 PowerShell 块curl --silent $JENKINS_URL/computer/$JENKINS_NODENAME/api/json
结果 json 包含 离线 属性 和 true/false 值