如何“立即扫描存储库";从Jenkinsfile [英] How to "Scan Repository Now" from a Jenkinsfile

查看:26
本文介绍了如何“立即扫描存储库";从Jenkinsfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 build 命令调用另一个詹金斯工作.有没有办法让我告诉另一项工作进行分支扫描?

I can call another jenkins job using the build command. Is there a way I can tell another job to do a branch scan?

多分支管道作业具有UI按钮立即扫描存储库".当您按下此按钮时,它将签出已配置的SCM存储库,并检测所有分支并为每个分支创建子作业.

A multibranch pipeline job has a UI button "Scan Repository Now". When you press this button, it will do a checkout of the configured SCM repository and detect all the branches and create subjobs for each branch.

我有一个多分支管道作业,因此我选择了抑制自动SCM触发"选项,因为我只希望它在从另一个作业中调用时才运行.因为选择了此选项,所以多分支管道不会自动检测何时将新分支添加到存储库.(如果我在用户界面中单击立即扫描存储库",它将检测到它们.)

I have a multibranch pipeline job for which I have selected the "Suppress automatic SCM triggering" option because I only want it to run when I call it from another job. Because this option is selected, the multibranch pipeline doesn't automatically detect when new branches are added to the repository. (If I click "Scan Repository Now" in the UI it will detect them.)

基本上,我有一个多分支管道作业,我想从另一个使用相同git存储库的多分支管道作业中调用它.

Essentially I have a multibranch pipeline job and I want to call it from another multibranch pipeline job that uses the same git repository.

node {
  if(env.BRANCH_NAME == "the-branch-I-want" && other_criteria) {
    //scanScm "../my-other-multibranch-job" <--- scanScm is a fake command I made up
    build "../my-other-multibranch-job/${env.BRANCH_NAME}"

在该 build 行上出现错误,因为目标多分支管道作业尚不知道 BRANCH_NAME 存在.我需要一种从当前作业中触发目标作业中的SCM重新扫描的方法.

I get an error on that build line, because the target multibranch pipeline job does not yet know that BRANCH_NAME exists. I need a way to trigger an SCM re-scan in the target job from this current job.

推荐答案

在发布问题后不久就解决了这个问题.针对基本的多分支管道作业而不是分支调用 build 会使它重新扫描.我上面的代码片段的解决方案最终看起来像是...

Ended up figuring this out shortly after posting the question. Calling build against the base multibranch pipeline job as opposed to a branch causes it to re-scan. The solution to my above snippet would have ended up looking something like...

node {
  if(env.BRANCH_NAME == "the-branch-I-want" && other_criteria) {
    build job: "../my-other-multibranch-job", wait: false, propagate: false // scan for branches
    sleep 2 // scanning takes time
    build "../my-other-multibranch-job/${env.BRANCH_NAME}"

wait:false 很重要,因为否则会出现错误:不支持等待非工作项目".多分支父"作业比作业更靠近文件夹,但是它是支持 build 命令的文件夹,并且可以通过扫描SCM来实现.

The wait: false is important because otherwise you get "ERROR: Waiting for non-job items is not supported". The multibranch "parent" job is closer to a folder than a job, but it's a folder that supports the build command, and it does so by scanning the SCM.

但是解决这个问题只会导致另一个问题,那就是使用 wait:false 我们无法知道SCM扫描何时完成.如果您有一个大型存储库(或者您缺少jenkins代理),则由于该分支不存在,直到第二个 build 命令已经失败之后,该分支才会被发现.您可以将睡眠时间提高得更高,但这并不能扩展.

But solving this just led to another problem, which is that with wait: false we have no way of knowing when the SCM Scan finished. If you have a large repository (or you're short on jenkins agents), the branch won't get discovered until after the second build command has already failed due to the branch not existing. You could bump the sleep time even higher, but that doesn't scale.

幸运的是,事实证明,如果您为詹金斯设置了github webhooks,则甚至不需要手动启动SCM扫描.分支将或多或少被立即发现,因此出于我的目的,这是另一种解决方法.我碰到它的原因是我们的开发詹金斯中没有设置webhooks,但是一旦我将此代码移到产品中,它就会很好地工作.

Fortunately, it turns out manually initiating the SCM scan isn't even needed if you have github webhooks set up for your jenkins. The branch will be discovered more-or-less instantly, so for my purposes this is solved another way. The reason I was running into it is we don't have webhooks set up in our dev jenkins, but once I move this code to prod it will work fine.

如果您尝试使用JobDSL来设置调用多分支的多分支,并且没有webhook或类似的东西,那么更好的路径可能是放弃第二层工作的多分支,并使用JobDSL创建文件夹和管理分支机构自己动手.

If you're trying to use JobDSL to set up multibranches calling multibranches and you don't have webhooks or something equivalent, the better path is probably to abandon multibranch for your second tier of jobs and use JobDSL to create folders and manage the branch jobs yourself.

这篇关于如何“立即扫描存储库";从Jenkinsfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆