简体   繁体   English

如何使用Jenkinsfile禁用Multibranch管道中的分支

[英]How to disable branch in Multibranch pipeline with Jenkinsfile

I have a Multi branch pipeline project in Jenkins. 我在Jenkins有一个Multi分支管道项目。 I want to disable a branch while it is listed in pipeline project. 我想在管道项目中列出一个分支时禁用它。 I can add an exception to surpass scm triggering. 我可以添加一个例外来超越scm触发。 But I want to disable all the triggering including manual triggering. 但我想禁用所有触发,包括手动触发。 If I used "Disable this project" under "Build triggers" in job created for a branch, that option is not selected when I reload the page(There are no save/apply buttons which is available for single branch pipelines). 如果我在为分支创建的作业中的“构建触发器”下使用“禁用此项目”,则在重新加载页面时不会选择该选项(没有可用于单个分支管道的保存/应用按钮)。 It only keeps follwing configuration that was configured in Jenkinsfile. 它只保留在Jenkinsfile中配置的后续配置。

pipelineTriggers([
    snapshotDependencies(),
]),

Are there any way to specify "Disable this project" in Jenkinsfile 有没有办法在Jenkinsfile中指定“禁用此项目”

True, you cannot disable the project from Jenkins Job as multibranch pipeline doesn't give control to change the settings from the job . 没错, 您无法从Jenkins Job禁用项目,因为多分支管道无法控制从作业更改设置

Two ways to stop your branch from building. 阻止你的分支建立的两种方法。

1) Disable the Branch 1) 禁用分支

To get control of the settings, go to PROJECT(project you want to modify the settings) > Configure > Projects( enable advanced settings) 要控制设置,请转到PROJECT(要修改设置的项目)>配置>项目(启用高级设置)

在此输入图像描述

Enter the branch in "Exclude branch" and save the settings. “排除分支”中输入分支并保存设置。

2) Rename Jenkinsfile 2) 重命名Jenkinsfile

If you don't have control of the project settings, the easy way is to rename the Jenkinsfile in the project/branch . 如果您无法控制项目设置,则可以轻松地在项目/分支中重命名Jenkins文件

在此输入图像描述

This configuration will define to trigger a build if a branch/project has "Jenkinsfile" in it. 如果分支/项目中包含“Jenkinsfile”,则此配置将定义为触发构建。 Hence, renaming it will not trigger a build. 因此,重命名它不会触发构建。

Hope this helps.. 希望这可以帮助..

UPDATE : AFAIK, you can't disable the project from Jenkisfile , but as workaround you can configure the cronjob as follows: 更新 :AFAIK, 您无法从Jenkisfile禁用该项目 ,但作为解决方法,您可以按如下方式配置cronjob:

properties([pipelineTriggers([cron('')])])

If configuration is not available in cron, the build will not trigger at all. 如果cron中没有配置,则构建将根本不会触发。

You can do something like this: 你可以这样做:

    stages {
    stage('Build') {
        when { 
            not { 
                branch 'master' 
            }
        }
        steps {
            ...
        }
    }
}

Which isn't quite the same as not running the pipeline at all, but at least, it runs very quickly, and doesn't actually do the build, which is useful to have it not fail - say, if you are using a maven / gitflow pattern, where the master branch should not be built more than once, since the deployment would fail... 这与完全不运行管道并不完全相同,但至少,它运行得非常快,并且实际上并没有进行构建,这对于让它不会失败很有用 - 比方说,如果你使用的是maven / gitflow模式,其中主分支不应该多次构建,因为部署将失败...

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

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