本文介绍了Jenkins 管道中的 deleteDir、cleanWs 和 'WsCleanup' 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都被提及(例如 在这个 SO 问题中)用于清理 Jenkinsfile.但是,似乎有些已过时或功能略有不同,我想了解使用哪个.

These have all been mentioned (for example in this SO question) for cleaning up the workspace in Jenkinsfile. However, it seems that some are obsolete or have slightly different function and I would like to understand which to use.

其中,deleteDir 是最常被提及的,显然其他只是调用 Jenkins Workspace Cleanup Plugin 的不同语法.

Of these, deleteDir is the most commonly mentioned, and apparently the others are just different syntaxes for invoking the Jenkins Workspace Cleanup Plugin.

功能上有什么区别?推荐哪个?

What is the functional difference? Which is recommended?

deleteDir()
cleanWs()
step([$class: 'WsCleanup'])

推荐答案

来自官方文档:

deleteDir:从工作区递归删除当前目录.递归删除当前目录及其内容.符号链接和交汇点将不被遵循,但将被删除.要删除工作区的特定目录,请将 deleteDir 步骤包装在 dir 步骤中.

所以,deleteDirWorkflow Basic Steps 插件(Pipeline Plugin 的一个组件).

So, deleteDir is a method of Workflow Basic Steps plugin (which is a component of Pipeline Plugin).

cleanWs:构建完成后删除工作区.

似乎 cleanWs() 只是 .

Seems to be that cleanWs() is just a new version of step([$class: 'WsCleanup']) from Workspace Cleanup Plugin.

据我了解,deleteDircleanWs 之间略有不同:cleanWs 有更多选项(如 cleanWhenAborted, cleanWhenFailure 等),使用起来更灵活,但建议仅在构建完成时使用(不确定是否可以在构建执行开始时使用).另一方面,我们可以使用 deleteDir 步骤在构建执行之前擦除工作空间.

As I understand, between deleteDir and cleanWs is a slightly difference: cleanWs has more options (like cleanWhenAborted, cleanWhenFailure, etc.) and it's more flexible to use, but it's recommended to use only when build is done (not sure if we can use it at the beginning of build execution). On the other side, we can use deleteDir step to wipe the workspace before build execution.

更新 1:
构建后 cleanWs 步骤也可以考虑构建状态,这就是为什么它应该只在构建执行之后使用.
但是,在 ws-cleanup 插件下也有 preBuildCleanup 步骤.您可以在 preBuildCleanup
cleanWs 检查示例 (DSL)"noreferrer">插件页面.

UPDATE 1:
The post build cleanWs step can also take into account the build status, that's why it should be used only after the build execution.
However, under ws-cleanup plugin there is preBuildCleanup step as well. You can check an example (DSL) with both preBuildCleanup and cleanWs on the plugin page.

更新 2:
@aaron-d-marasco 指出最好不要在 docker 镜像中使用 deleteDir .您可以在这个打开的错误中查看详细信息.

UPDATE 2:
@aaron-d-marasco pointed out that it's better not to use deleteDir in a docker image.You can check the details in this open bug.

这篇关于Jenkins 管道中的 deleteDir、cleanWs 和 'WsCleanup' 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:24