我有一个Jenkins管道,在这里我实现了一些阶段。Jenkins在作业运行时输出一些消息。
代码:
stage('My stage') {
options {
timeout(time: 2, unit: "MINUTES")
}
steps {
script {
./script.sh
}
}
}
}
输出:
...
20:12:40 Timeout set to expire in 2 min 0 sec
[Pipeline] {
[Pipeline] script
[Pipeline] {
20:12:40 ./script.sh '
20:14:40 Cancelling nested steps due to timeout
20:14:40 Sending interrupt signal to process
20:14:48 ...
20:14:48 + JOB_NAME=...
20:14:48 + BUILD_URL=...
20:14:48 + BUILD_ID=567
20:14:48 + BUILD_RESULT=SUCCESS
在本例中,我正处于一项任务的中间,并且显示了许多消息。我需要捕获这条消息“由于超时而取消嵌套步骤”。当jenkins输出此消息时,我希望能够向用户发送通知。
我不知道该怎么做。
发布于 2022-08-12 19:38:39
在执行script.sh
之后,您可以像下面这样检查控制台日志。
stage('My stage') {
options {
timeout(time: 2, unit: "MINUTES")
}
steps {
script {
sh "./script.sh"
// Checking the console log
def consoleLog = Jenkins.getInstance().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).logFile.text
if(consoleLog.contains("Cancelling nested steps due to timeout")) {
echo "Send the Notification"
}
}
}
}
}
另外,如果您不想按顺序执行此操作,则可以创建一个并行阶段并继续检查日志。
https://stackoverflow.com/questions/73335068
复制相似问题