简体   繁体   English

为什么 Jenkins 允许指定多个 git 分支?

[英]Why does Jenkins allow specifying multiple git branches?

Multiple times now I have stumbled over why does Jenkins support multiple "branches to build", considering that the help text does not recommend it, what would be a valid use-case?现在我多次偶然发现为什么 Jenkins 支持多个“要构建的分支”,考虑到帮助文本不推荐它,什么是有效的用例?

jenkins git scm 配置中的多个分支

Edit: I am referring to why there is the "Add branch" button in a single Job, not Multi-Branch.编辑:我指的是为什么单个作业中有“添加分支”按钮,而不是多分支。 jenkins git scm 配置中的多个分支

I cannot explain what a use-case would be, but from reading the source code there seems to be an "advanced use-case" in the git plugin which selects a single branch* (ref) and schedules a new build for the other ones.我无法解释用例是什么,但是从阅读源代码来看,git 插件中似乎有一个“高级用例”,它选择一个分支*(参考)并为其他分支安排一个新的构建.

It must be noted that scheduling new builds is limited to jobs whose implementation inherits from AbstractProject , like Free-Style Jobs.必须注意的是,调度新构建仅限于其实现继承自AbstractProject的作业,例如 Free-Style Jobs。 Note well that Pipeline Jobs are not AbstractProjects !请注意,管道作业不是AbstractProjects

* There are "build choosers" available which order or limit the matched branches/refs. * 有可用的“构建选择器”来排序或限制匹配的分支/引用。

Selection code excerpt:选择代码摘录:

    /**
     * If the configuration is such that we are tracking just one branch of one repository
     * return that branch specifier (in the form of something like "origin/master" or a SHA1-hash
     *
     * Otherwise return [@code null}.
     */
    @CheckForNull
    private String getSingleBranch(EnvVars env) {
        // if we have multiple branches skip to advanced usecase
        if (getBranches().size() != 1) {
            return null;
        }

        // [...]
    }

    // [...]

    /**
     * Determines the commit to be built in this round [...]
     */
    private @NonNull Build determineRevisionToBuild(/* ... */) {

        // [...]
    
        if (candidates.isEmpty() ) {
            final String singleBranch = environment.expand( getSingleBranch(environment) );


            candidates = getBuildChooser().getCandidateRevisions(
                    false, singleBranch, git, listener, buildData, context);
        }


        if (candidates.isEmpty()) {
            // getBuildCandidates should make the last item the last build, so a re-build
            // will build the last built thing.
            throw new AbortException("Couldn't find any revision to build. Verify the repository and branch configuration for this job.");
        }


        Revision marked = candidates.iterator().next();

( source ) 来源

scheduling new build:安排新的构建:

        if (candidates.size() > 1) {
            log.println("Multiple candidate revisions");
            if (checkForMultipleRevisions) {
                Job<?, ?> job = build.getParent();
                if (job instanceof AbstractProject) {
                    AbstractProject project = (AbstractProject) job;
                    if (!project.isDisabled()) {
                        log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
                        if (!project.scheduleBuild(0, new SCMTrigger.SCMTriggerCause("This build was triggered by build "
                                + build.getNumber() + " because more than one build candidate was found."))) {
                            log.println("WARNING: multiple candidate revisions, but unable to schedule build of " + project.getFullDisplayName());
                        }
                    }
                }
            }
        }

( source ) 来源

You have a more descriptive explanation in the latest version of the plugin.您在最新版本的插件中有更具描述性的解释。

在此处输入图像描述

Let me answer your question.让我回答你的问题。 This really boils down to the question why would you want to create multiple branches in Git?这真的归结为一个问题,为什么要在 Git 中创建多个分支? A typical example that is applicable to a CICD pipeline is.适用于 CICD 管道的典型示例是。 Let's say you have some code that will be deployed to different environments.假设您有一些将部署到不同环境的代码。 So inorder to represent different environments you can create branches.因此,为了表示不同的环境,您可以创建分支。 For example develop , test , production etc. Developers will work on develop branch when the development is completed it will be propagated upto production.例如developtestproduction等。开发完成后,开发人员将在开发分支上工作,它将传播到生产。 Develop branch will be merged to test and then to production.开发分支将被合并到测试,然后到生产。 This is where Jenkins comes into the picture.这就是詹金斯出现的地方。 On each commit to the relevant branch you need to deploy this code to a relevant environment and to do this you can create a single pipeline to handle the deployment process.在每次提交到相关分支时,您需要将此代码部署到相关环境,为此,您可以创建一个管道来处理部署过程。

For example look at the following pipeline.例如看下面的管道。

 stage('Deliver for development') {
            when {
                branch 'development'
            }
            steps {
                sh './jenkins/scripts/deliver-for-development.sh'
                input message: 'Finished using the web site? (Click "Proceed" to continue)'
                sh './jenkins/scripts/kill.sh'
            }
        }
        stage('Deploy for production') {
            when {
                branch 'production'
            }
            steps {
                sh './jenkins/scripts/deploy-for-production.sh'
                input message: 'Finished using the web site? (Click "Proceed" to continue)'
                sh './jenkins/scripts/kill.sh'
            }
        }

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

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