自定义Jenkins Stop [x]按钮功能

时间:2015-10-26 14:55:16

标签: jenkins abort

我的詹金斯建立了许多工作,这些工作没有可见性。

如果我通过按停止按钮中止构建,则作业将继续,直到我启动新构建,删除工作区,导致所有作业都因为目录不再存在而翻倒。

这不太理想,所以我想知道是否可以在停止按钮上添加一些功能来杀死这些工作?

注意,我指的是this button

2 个答案:

答案 0 :(得分:1)

我刚刚创建:

  • 构建执行shell →命令:

    Freestyle项目
    #!/bin/bash
    
    while true
    do
      echo "  SO-33348825-sub-1 running eternally..."
      sleep 10
    done
    
  • 带有工作流程的→Workflow项目→定义→脚本:

    println("  SO-33348825-main-workflow started")
    build("SO-33348825-sub-1")
    println("  SO-33348825-main-workflow ended")
    

启动并中止 SO-33348825-main-workflow 的日志:

...
  SO-33348825-main-workflow started
Running: Building SO-33348825-sub-1
Starting building project: SO-33348825-sub-1
Aborted by anonymous
Running: End of Workflow
java.lang.Exception: ABORTED
    at org.jenkinsci.plugins.workflow.support.steps.build.BuildTriggerListener.onCompleted(BuildTriggerListener.java:20)
    at hudson.model.listeners.RunListener.fireCompleted(RunListener.java:201)
    at hudson.model.Run.execute(Run.java:1786)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:408)
Finished: ABORTED

在启动和中止 SO-33348825-main-workflow SO-33348825-sub-1 的日志:

Started by upstream project "SO-33348825-main-workflow" build number 1
...
[workspace] $ /bin/bash /tmp/hudson1610853144078403465.sh
  SO-33348825-sub-1 running eternally...
  SO-33348825-sub-1 running eternally...
  SO-33348825-sub-1 running eternally...
Build was aborted
Calling workflow was cancelled
...
Finished: ABORTED

结论

如果工作流构建中止,则工作流项目启动的构建将中止。

答案 1 :(得分:1)

使用外部作业(可以使用作业类型External Job监控BTW),您可以执行以下操作:

  • 创建parameterized自由式作业。

  • 配置构建号。作为参数输入,例如NUMBER_OF_BUILD_TO_ABORT,在工作开始时。

  • 添加执行shell 执行Groovy脚本 构建步骤,其中包含:

     
    ... code to kill the external jobs ...
    
    ... code that calls http://jenkins/job/<job name>/${NUMBER_OF_BUILD_TO_ABORT}/stop ...