解决 Jenkins 报错 git checkout failed fatal: Unable to create index.lock 修复
排查 Jenkins 拉取代码抛出 fatal: Unable to create '.git/index.lock': File exists,分析构建被强制杀掉残留的孤儿锁文件。
问题现象
Pipeline 在第一步源码检出时失败退出:
> git checkout -f e1234567890abcdef
fatal: Unable to create '/var/lib/jenkins/workspace/my-job/.git/index.lock': File exists.
Another git process seems to be running in this repository...
触发根因
- 先前的构建任务被管理员强制中止或服务器突发断电,Git 进程在退出前未能删除临时排他锁
index.lock; - 多个并行运行的 Job 被错误地配置到了同一个共享工作区路径。
解决步骤
在 Git Checkout 插件选项中勾选检出前清理(Clean before checkout)
在流水线配置中添加:
checkout([
$class: 'GitSCM',
extensions: [[$class: 'CleanBeforeCheckout']],
userRemoteConfigs: [[url: 'git@github.com:org/repo.git']]
])
手动清理工作区内的孤儿锁文件
在构建服务器上执行删除:
rm -f /var/lib/jenkins/workspace/my-job/.git/index.lock
验证确认
重新触发构建:
代码检出毫秒级走通,工作区干净就绪无锁竞争异常。


