简体   繁体   English

如何通过 Jenkinsfile 克隆到潜在的非空目录?

[英]How do I clone into a potentially non-empty directory via Jenkinsfile?

I've got a simple Jenkinsfile that clones a Git repo down into one of a number of agents.我有一个简单的 Jenkinsfile,它将 Git 存储库克隆到多个代理之一中。 These agents may not have the repo already, or it may.这些代理可能还没有回购,或者可能。

Currently here's the Jenkinsfile:目前这里是 Jenkinsfile:

stage("Clone Postman Collection") {
       steps {
           sh "git clone ssh://git@bitbucket.company.com/lol/postman-scripts.git"
       }
   }

Currently this works the first time the repo is cloned, but if the folder isn't empty it causes the job to fail with a pretty understandable error: fatal: destination path 'postman-scripts' already exists and is not an empty directory.目前这在第一次克隆 repo 时有效,但如果文件夹不为空,则会导致作业失败,并出现一个非常容易理解的错误: fatal: destination path 'postman-scripts' already exists and is not an empty directory.

The reason I'm asking this question instead of using one of the other answers from the multitudes of other Stack Overflow questions is that I can't be certain if the Agent this is running on actually has the repo already cloned or not, so there needs to be some sort of IF clause or something.我问这个问题而不是使用其他许多 Stack Overflow 问题中的其他答案之一的原因是,我无法确定正在运行的代理是否实际上已经克隆了 repo,所以那里需要某种IF子句之类的。

I'm wondering if there is an elegant way to handle this in the Jenkinsfile.我想知道在 Jenkinsfile 中是否有一种优雅的方式来处理这个问题。 For example, can I:例如,我可以:

Force the clone to overwrite files it finds?强制克隆覆盖它找到的文件?

Cause the error to be non-fatal?导致错误不是致命的?

Catch the specific error and delete, retry?捕捉到具体错误并删除,重试?

Force the clone to overwrite files it finds?强制克隆覆盖它找到的文件?

The logical conclusion of this line of thought is to delete the workspace or clone every time.这种思路的逻辑结论是每次都删除工作空间或克隆。 There's often good reasons to do this anyway, one of the more compelling of which is "your build process is going to create artifacts that are not going to be in the repo".无论如何,这样做通常有很好的理由,其中一个更引人注目的是“您的构建过程将创建不会出现在 repo 中的工件”。

But if the repo is big enough I suppose you might really want the cache, so但是如果 repo 足够大,我想你可能真的想要缓存,所以

Cause the error to be non-fatal?导致错误不是致命的? Catch the specific error and delete, retry?捕捉到具体错误并删除,重试?

You're already running shell script, so you could use shell constructs to add some logic between the result of git clone and the next steps.您已经在运行 shell 脚本,因此您可以使用 shell 构造在git clone的结果和后续步骤之间添加一些逻辑。 It seems straight forward to use the destination directory as the flag for whether you clone or fetch / checkout - if the directory exists, clone, otherwise, checkout.使用目标目录作为克隆或获取/签出的标志似乎很简单——如果目录存在,则克隆,否则,签出。

But I recommend against this.但我建议不要这样做。 Your repos is only going to take a few seconds to clone in your current setup, I'd wager, and your time is worth way more than jenkins's.我敢打赌,您的存储库只需要几秒钟就可以克隆到您当前的设置中,而且您的时间比詹金斯的时间更值得。 Delete the repo on every checkout and you lose very little and gain a much simpler runtime consistency guarantee.在每次结帐时删除 repo,您损失的很少,并获得更简单的运行时一致性保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM