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

问题描述 投票:0回答:2

现在我多次偶然发现为什么 Jenkins 支持多个“构建分支”,考虑到帮助文本不推荐它,什么是有效的用例?

multiple branches in jenkins git scm configuration

编辑:我指的是为什么在单个作业中有“添加分支”按钮,而不是在多分支中。 multiple branches in jenkins git scm configuration

git jenkins continuous-integration
2个回答
2
投票

我无法解释用例是什么,但从阅读源代码来看,git 插件中似乎有一个“高级用例”,它选择一个分支*(参考)并为另一个分支安排一个新的构建那些。

必须注意的是,调度新构建仅限于其实现继承自

AbstractProject
的作业,例如自由式作业。 请注意,管道作业不是
AbstractProjects

* 有可用的“构建选择器”,可以排序或限制匹配的分支/引用。

选型代码摘录:

    /**
     * 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();

来源

安排新的构建:

        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());
                        }
                    }
                }
            }
        }

来源


0
投票

您在最新版本的插件中有更具描述性的解释。

enter image description here

我来回答你的问题。这实际上可以归结为一个问题:为什么要在 Git 中创建多个分支?适用于 CICD 管道的典型示例是。假设您有一些代码将部署到不同的环境。因此,为了代表不同的环境,您可以创建分支。例如

develop
test
production
等。开发人员将在开发分支上工作,当开发完成后,它将传播到生产环境。开发分支将合并到测试,然后合并到生产。这就是詹金斯发挥作用的地方。在每次提交到相关分支时,您需要将此代码部署到相关环境,为此,您可以创建一个管道来处理部署过程。

例如,看看下面的管道。

 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'
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.