如何优雅移除Jenkins中已无对应构建与Pod的卡住Executor
解决Jenkins Kubernetes插件场景下卡住执行器的优雅方案
问题场景
Jenkins流水线通过Kubernetes插件运行在Pod中,触发以下操作后出现执行器卡死:
- 构建因历史轮转配置被自动删除
- 对应Kubernetes Pod被手动删除
此时出现的异常现象:
- 该构建条目仍显示在执行器状态列表中
- 调用
WorkflowRun.finish()时抛出java.lang.NullPointerException - 即使构建已不存在,
Executor.isBusy()和Executor.isActive()仍返回true - 关联的
SubTask为org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution.PlaceholderTask,且SubTask.isContinued()返回true
优雅清理方案
1. 脚本控制台精准清理(推荐)
通过Jenkins脚本控制台(路径:Manage Jenkins > Script Console)执行以下Groovy脚本,自动定位并释放卡住的执行器,无需重启Jenkins:
import hudson.model.Executor import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution import jenkins.model.Jenkins // 遍历所有节点的常规执行器 Jenkins.instance.nodes.each { node -> node.executors.each { executor -> if (executor.isBusy() && executor.currentExecutable instanceof ExecutorStepExecution.PlaceholderTask) { def stuckTask = executor.currentExecutable stuckTask.finish(null, null) executor.interrupt(hudson.model.Result.ABORTED) println "[清理完成] 节点 ${node.displayName} 上的执行器已释放" } } // 处理节点的临时执行器 node.oneOffExecutors.each { executor -> if (executor.isBusy() && executor.currentExecutable instanceof ExecutorStepExecution.PlaceholderTask) { def stuckTask = executor.currentExecutable stuckTask.finish(null, null) executor.interrupt(hudson.model.Result.ABORTED) println "[清理完成] 节点 ${node.displayName} 上的临时执行器已释放" } } }
执行完成后刷新Jenkins页面,卡住的执行器状态会自动恢复正常。
2. 清理残留WorkflowRun实例
如果内存中仍存在已删除构建的WorkflowRun残留实例,可执行以下脚本清理关联执行器:
import org.jenkinsci.plugins.workflow.job.WorkflowRun import jenkins.model.Jenkins Jenkins.instance.getAllItems(WorkflowRun).each { run -> if (!run.isBuilding() && run.executor != null && run.executor.currentExecutable instanceof ExecutorStepExecution.PlaceholderTask) { run.finish(hudson.model.Result.ABORTED, new java.io.IOException("清理残留卡住执行器")) println "[清理完成] 残留构建 ${run.fullDisplayName} 对应的执行器已释放" } }
预防措施
- 调整构建历史轮转策略:确保在删除构建前,先通过Jenkins触发Pod终止,避免手动删除Pod后残留执行器
- 配置Kubernetes插件Pod超时:在Kubernetes插件配置中设置Pod的活跃超时时间,自动回收无响应的Pod
- 升级插件版本:安装
Pipeline: Nodes and Processes插件的最新版本,优化PlaceholderTask的生命周期处理逻辑
内容的提问来源于stack exchange,提问作者Marslo
相关产品推荐
相关产品推荐

