从批处理脚本中提升Jenkins Job中的Abort

时间:2018-05-15 06:42:30

标签: jenkins jenkins-plugins

我有一个Jenkins工作,它可以执行Git同步并构建源代码。

我添加并创建了一个" Post构建任务"选项。

在'构建后任务'中,我正在搜索关键字" TIMEOUT:"在控制台输出中(此部分已完成),并且如果关键字匹配,则要将作业声明为失败和中止。

如果关键字匹配,如何从批处理脚本中提取/声明作业中止。像echo ABORT

这样的东西

2 个答案:

答案 0 :(得分:0)

您可以直接退出流程并引发所需的错误代码:

echo "Timeout detected!"
exit 1

Jenkins应检测错误并将构建设置为失败。

错误代码必须介于1到255之间。您可以选择任意内容,只需注意保留一些代码: http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF

您还可以考虑使用超时插件: https://wiki.jenkins.io/display/JENKINS/Build-timeout+Plugin

另一种选择是为BUILD ID URL/stop构建查询。这正是您手动中止构建时所执行的操作。

echo "Timeout detected!"
curl yourjenkins/job_name/11/stop

答案 1 :(得分:0)

如果您想将其标记为" FAIL"

,则会更容易

只需退出1即可。

实现" Abort"是很棘手的。从post build任务插件开始,使用Groovy post build插件要容易得多。

groovy post build提供了丰富的功能来帮助你。

如匹配功能:

def matcher = manager.getLogMatcher(".*Total time: (.*)\$")
if(matcher?.matches()) {
    manager.addShortText(matcher.group(1), "grey", "white", "0px", "white")
}

中止功能:

  def executor = build.executor ?: build.oneOffExecutor;
  if (executor != null){
      executor.interrupt(Result.ABORTED)
  }

BR,