You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

手动中止Jenkins构建时,如何终止TestExecute进程?

Jenkins中止构建时终止TestExecute进程的解决方案

默认情况下,点击Jenkins的红色X按钮手动中止构建,只会将构建标记为"已中止"状态并释放执行器资源,不会自动终止构建过程中启动的子进程(比如TestExecute),这就是你遇到的进程残留、持续执行测试的问题。

以下是几种可行的解决方法:

1. 启用Jenkins自带的进程树清理机制

Jenkins内置了ProcessTreeKiller功能,可在构建中止时尝试清理关联的子进程树,配置方式如下:

  • 全局配置:进入Manage Jenkins -> Configure System,找到"Process Killer"相关选项,勾选Kill process trees on job cancellation(不同Jenkins版本选项名称可能略有差异)。
  • 注意事项:
    • 对于Windows系统,避免使用start命令启动TestExecute(该命令会让进程脱离Jenkins的进程组,导致无法被自动清理),直接在构建步骤中调用TestExecute的可执行文件即可。
    • Linux/macOS系统下,该机制依赖进程组关联,只要TestExecute是Jenkins构建进程直接启动的子进程,就能被自动终止。

2. 在构建脚本中手动处理中止信号

通过脚本监听Jenkins的中止信号,收到信号时主动杀掉TestExecute进程,以下是不同脚本的示例:

PowerShell脚本示例

# 启动TestExecute并记录进程ID
$testProc = Start-Process -FilePath "C:\Program Files (x86)\SmartBear\TestExecute 15\Bin\TestExecute.exe" -ArgumentList "/run C:\Tests\MyTestProject.pjs /exit" -PassThru

# 注册中止信号处理程序,收到中断时强制终止TestExecute
$cancelEvent = Register-ObjectEvent -InputObject [console]::CancelKeyPress -EventName CancelKeyPress -Action {
    Stop-Process -Id $testProc.Id -Force
    exit 1
} | Out-Null

# 等待TestExecute执行完成
Wait-Process -Id $testProc.Id

Windows批处理脚本示例

@echo off
# 直接启动TestExecute(不要用start命令)
"C:\Program Files (x86)\SmartBear\TestExecute 15\Bin\TestExecute.exe" /run C:\Tests\MyTestProject.pjs /exit

# 如果构建被中止,Jenkins会触发脚本中断,这里添加兜底的进程清理
taskkill /f /im TestExecute.exe

Jenkins Pipeline(Groovy)示例

在流水线的post阶段添加中止时的处理逻辑,确保无论构建是否被中止,都能清理进程:

pipeline {
    agent any
    stages {
        stage('Execute Tests') {
            steps {
                script {
                    // 启动TestExecute并等待完成
                    bat(script: '"C:\\Program Files (x86)\\SmartBear\\TestExecute 15\\Bin\\TestExecute.exe" /run C:\\Tests\\MyTestProject.pjs /exit', returnStatus: true)
                }
            }
        }
    }
    post {
        // 构建被中止时执行进程清理
        aborted {
            bat 'taskkill /f /im TestExecute.exe'
        }
        // 也可以添加always阶段的清理,确保进程不会残留
        always {
            bat 'taskkill /f /im TestExecute.exe 2>nul'
        }
    }
}

3. 使用Jenkins插件增强进程控制

可以借助第三方插件实现更灵活的进程管理:

  • Process Killer Plugin:允许在构建的不同阶段(包括中止时)杀掉指定名称或PID的进程。
  • Build Failure Analyzer:除了分析构建失败原因,也可配置在构建中止时执行自定义的进程清理命令。

内容的提问来源于stack exchange,提问作者preem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:37:32