Jenkins流水线清理工作区失败求助:无法删除文件问题排查
Jenkins流水线清理工作空间失败问题分析
问题描述
构建Jenkins任务时,流水线因无法删除工作空间文件执行失败,相同代码逻辑在其他任务中可正常运行,无法定位问题根源,请求分析错误产生的原因。
错误日志
ERROR: Failed to clean the workspace jenkins.util.io.CompositeIOException: Unable to delete '/var/lib/jenkins/workspace/QSearch_RC'. Tried 3 times (of a maximum of 3) waiting 0.1 sec between attempts. (Discarded 112 additional exceptions) at jenkins.util.io.PathRemover.forceRemoveDirectoryContents(PathRemover.java:90) at hudson.Util.deleteContentsRecursive(Util.java:262) at hudson.Util.deleteContentsRecursive(Util.java:251) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:778) at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1223) at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1301) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:129) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:97) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:84) at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:750) Suppressed: java.nio.file.AccessDeniedException: /var/lib/jenkins/workspace/QSearch_RC/build/hirl/qsrch.full.prod.SunOS5.10.WS12.0 at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) at sun.nio.fs.UnixFileAttributeViews$Posix.setMode(UnixFileAttributeViews.java:238) at sun.nio.fs.UnixFileAttributeViews$Posix.setPermissions(UnixFileAttributeViews.java:260) at java.nio.file.Files.setPosixFilePermissions(Files.java:2045) at jenkins.util.io.PathRemover.makeWritable(PathRemover.java:286) at jenkins.util.io.PathRemover.makeRemovable(PathRemover.java:259) at jenkins.util.io.PathRemover.removeOrMakeRemovableThenRemove(PathRemover.java:239) at jenkins.util.io.PathRemover.tryRemoveFile(PathRemover.java:205) at jenkins.util.io.PathRemover.tryRemoveRecursive(PathRemover.java:216) at jenkins.util.io.PathRemover.tryRemoveDirectoryContents(PathRemover.java:226) at jenkins.util.io.PathRemover.tryRemoveRecursive(PathRemover.java:215) at jenkins.util.io.PathRemover.tryRemoveDirectoryContents(PathRemover.java:226) at jenkins.util.io.PathRemover.tryRemoveRecursive(PathRemover.java:215) at jenkins.util.io.PathRemover.tryRemoveDirectoryContents(PathRemover.java:226) at jenkins.util.io.PathRemover.forceRemoveDirectoryContents(PathRemover.java:87) ... 14 more [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: Error cloning remote repo 'origin' Finished: FAILURE
操作步骤
- 尝试构建Jenkins任务
- 任务因上述错误执行失败
原因分析与解决建议
核心问题
错误日志明确显示AccessDeniedException,Jenkins进程没有权限修改或删除路径/var/lib/jenkins/workspace/QSearch_RC/build/hirl/qsrch.full.prod.SunOS5.10.WS12.0的文件。
可能原因
- 权限归属异常:该文件/目录的所有者或所属组不是Jenkins运行用户(通常为
jenkins),比如之前构建时使用root或其他用户生成文件,导致权限锁定。 - 文件被占用:该文件被系统进程(如正在运行的程序、日志工具)或第三方软件(如监控工具)占用,无法被删除。
- 权限设置极端:文件被设置了
chmod 000这类完全无权限的属性,Jenkins无法修改。 - 磁盘挂载限制:工作空间所在磁盘挂载时添加了
noexec、nosuid等限制修改文件属性的参数。
解决建议
检查并修复权限
登录Jenkins节点服务器,执行以下命令查看文件权限:ls -l /var/lib/jenkins/workspace/QSearch_RC/build/hirl/qsrch.full.prod.SunOS5.10.WS12.0若所有者不是
jenkins,执行以下命令修改:sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace/QSearch_RC sudo chmod -R u+rwX /var/lib/jenkins/workspace/QSearch_RC检查进程占用
用lsof命令查看是否有进程占用目标文件:lsof /var/lib/jenkins/workspace/QSearch_RC/build/hirl/qsrch.full.prod.SunOS5.10.WS12.0若存在占用进程,kill对应进程后重新构建。
临时跳过清理
在Jenkins任务的SCM配置中取消Clean before checkout选项,或在流水线中添加skipDefaultCheckout: true,后续再手动处理清理逻辑(如使用sudo rm -rf,需注意服务器安全)。检查磁盘挂载属性
执行以下命令查看磁盘挂载参数:mount | grep $(df -P /var/lib/jenkins/workspace | tail -1 | awk '{print $1}')确保挂载参数中没有
noexec等限制修改文件的设置。
内容的提问来源于stack exchange,提问作者Jagadeesh Pandranki
相关产品推荐
相关产品推荐

