简体   繁体   English

Jenkins 管道作业签出更改功能分支

[英]Jenkins pipeline job checkout changed feature branch

I have the following stage in my jenkins pipeline to get all branches of a project.我的 jenkins 管道中有以下阶段来获取项目的所有分支。 I would like to trigger a build job, if one branch in project has changed on the changed branch.如果项目中的一个分支在更改的分支上发生了更改,我想触发构建作业。

First detect the changed branch and then checkout this specific branch out (I know that a webhook from bitbucket/github to jenkins would work.).首先检测更改的分支,然后签出这个特定的分支(我知道从 bitbucket/github 到 jenkins 的 webhook 可以工作。)。

checkout all branches in project:签出项目中的所有分支:

    stage ("Code pull"){
        steps{
         checkout([
            $class: 'GitSCM', branches: [[name: '*/*']],
            extensions: [[$class: 'CleanCheckout']], 
            userRemoteConfigs: [[  credentialsId:'bitbucket', url: 'my_git_repo']] 
            ])

If I new the branch something like that works fine如果我新分支这样的工作正常

        stage ("Code pull"){
        steps{
             git branch: "master",
             credentialsId: 'bitbucket',
             url: 'my_repo'
            }
    }

but I would like to test any changed branch in a specific repository.但我想测试特定存储库中任何更改的分支。 Maybe there is a way to iterate over all branches and something like that:也许有一种方法可以遍历所有分支和类似的东西:

 when { anyOf { branch 'feature-branch/*'; branch 'master' } }
    steps{
      ....git checkout the one with changes 
    }

Any hints will be appreciated.任何提示将不胜感激。 Thanks for help,感谢帮助,

best wishes Joern最好的祝愿乔恩

I propose to use a multibranch pipeline.我建议使用多分支管道。 This simplifies the work you would like to do.这简化了您想做的工作。 The multibranch pipeline keeps track of all your branches and builds only the ones that were changed.多分支管道跟踪您的所有分支并仅构建已更改的分支。 You can even filter out branches that doesn't need a build (eg feature branches).您甚至可以过滤掉不需要构建的分支(例如功能分支)。

在此处输入图像描述

After this, you can select the relevant branches.在此之后,您可以 select 相关分支。 You have multiple options to do this, now I attached one sample.您有多种选择来执行此操作,现在我附上了一个示例。 In case the branch is not part of the selected branches list, then the pipeline will not start.如果分支不是所选分支列表的一部分,则管道将不会启动。

在此处输入图像描述

In your Jenkinsfile, you just have to write def scmVars = checkout scm .在您的 Jenkinsfile 中,您只需编写def scmVars = checkout scm This command will clone the repository and checks out the branch that was changed.此命令将克隆存储库并签出已更改的分支。

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

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