詹金斯为什么允许指定多个git分支?

发布于 2025-02-08 12:47:26 字数 477 浏览 2 评论 0原文

现在,我多次偶然发现了为什么詹金斯(Jenkins)支持多个“要构建的分支”,因为帮助文本不建议它,这将是有效的用例?

edit:我是指为什么在单个作业中有“添加分支”按钮,而不是多分支。

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?

multiple branches in jenkins git scm configuration

Edit: I am referring to why there is the "Add branch" button in a single Job, not Multi-Branch.
multiple branches in jenkins git scm configuration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若相惜即相离 2025-02-15 12:47:26

我无法解释什么用例,但是通过读取源代码,Git插件中似乎有一个“高级用例”,该插件选择一个单个分支*(参考),并为其他构建安排新构建。

必须注意的是,安排新版本仅限于作业,其实现从AbstractProject(例如自由式作业)继承。请注意,管道作业不是AbstractProject

*有“构建选择器”可用,可以订购或限制匹配的分支/参考。

选择代码摘录:

    /**
     * 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

调度新构建:

        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

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.

It must be noted that scheduling new builds is limited to jobs whose implementation inherits from AbstractProject, like Free-Style Jobs. Note well that Pipeline Jobs are not 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)

风透绣罗衣 2025-02-15 12:47:26

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

让我回答您的问题。这确实归结为一个问题,为什么要在git中创建多个分支?适用于CICD管道的典型示例是。假设您有一些将部署到不同环境的代码。因此,在代表不同环境的订单中,您可以创建分支。例如,开发, test ,生产等。开发人员将在开发完成后在开发分支上工作,将在生产中传播。开发分支将合并以测试然后进行生产。这是詹金斯进入图片的地方。在对相关分支的每个提交中,您需要将此代码部署到相关环境并执行此操作,您可以创建单个管道来处理部署过程。

例如,查看以下管道。

 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'
            }
        }

You have a more descriptive explanation in the latest version of the plugin.

enter image description here

Let me answer your question. This really boils down to the question why would you want to create multiple branches in Git? A typical example that is applicable to a CICD pipeline is. 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. 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'
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文