如何为特定的 Jenkins 作业设置 `killSoftly`?

How to set `killSoftly` for a specific Jenkins job?

我的 Jenkins 构建在构建和 post-构建步骤之间挂起。

控制台输出显示有 6 分钟的等待时间(但我看到等待时间长达 1 小时):

10:53:26 BUILD FAILED in 1m 7s
10:53:26 4 actionable tasks: 4 executed
10:53:26 Build step 'Invoke Gradle script' changed build result to FAILURE
10:53:26 Build step 'Invoke Gradle script' marked build as failure
11:09:29 [CucumberReport] Using Cucumber Reports version 4.9.0

我发现 and this 个有类似问题的问题,他们说解决方案是在 jenkins.xml 中设置 -DSoftKillWaitSeconds=0

但是,我需要一种方法来仅为特定作业设置选项,而不会弄乱全局 Jenkins 设置(我不想弄乱其他项目)。

编辑:

当我手动中止作业时,在 [CucumberReport] 步骤之前,仍然会生成 Cucumber 报告。

我还选中了构建环境选项中的如果构建卡住则中止构建复选框,并将超时策略设置为No Activity(超时秒数= 2)。

当我使用这些设置构建项目时,构建将失败并在构建历史记录中显示 "Aborted after 0 seconds",与以前一样,但控制台输出将是相同的。 (没有任何变化,Cucumber 报告将在一定的超时后生成)。

不可能 select SoftKillWaitSeconds 的作业特定值(该值来自 [=12] 的 Jenkins 核心=] 其中工作名称未知)。

我的建议是修复作业本身的中止处理,这样它就不会依赖于"soft kill timeout"。如果您 运行 在 Unix-ish 系统上,您可以通过 运行 您在新进程组中的作业(bash 中的 set -m )和(例如) 设置适当的退出陷阱。

我们正在使用 Build-timeout 插件来终止超时策略设置为 No ActivityAbsolute 的卡住作业。对我来说,当您使用自由式项目时,这是一个很好的方法。 你的构建是 "Aborted after 0 seconds" 的原因是很可能有未完成的子进程。 来自 documentation:

Because Java only allows threads to be interrupted at a set of fixed locations, depending on how a build hangs, the abort operation might not take effect. For example,

  • if Jenkins is waiting for child processes to complete, it can abort right away.
  • if Jenkins is stuck in an infinite loop, it can never be aborted.
  • if Jenkins is doing a network or file I/O within the Java VM (such as lengthy file copy or SVN update), it cannot be aborted.

你可以试试绝对超时策略。您可以定义一个全局变量,这样您就不会在作业中重复超时值:

  1. 转到 "Manage Jenkins" > "Configure System"。
  2. 在 "Global properties" 中检查 "Environment variables"。
  3. 添加环境变量name="GLOBAL_TIMEOUT_MINUTES" value="20"。
  4. 转到项目的配置页面。
  5. 在 "Build Environment" 中检查 "Abort the build if it's stuck"。
  6. Select "Absolute" "Time-out strategy"。当然,也适用于其他策略。
  7. 为 "Timeout" 设置“${GLOBAL_TIMEOUT_MINUTES}”。
  8. 设置超时动作"Abort the build".

如果这不起作用,您可以尝试查看日志 https://your-jenkins-server/log 或线程转储。
挂起可能是由 new/old 版本的插件引起的。尝试找出哪些是未完成的子进程。尝试一项一项地禁用 post-build 操作,以找到可能导致问题的原因。 可以看到https://superuser.com/questions/1401879/debugging-what-happens-when-a-jenkins-build-unexpectedly-pauses-or-hangs