添加抑制自动 scm 触发,除了 Jenkins Job DSL 中的命名分支?

Adding Suppress automatic scm triggering except for named branches in Jenkins Job DSL?

如何在 Job DSL 中添加除命名分支之外的默认抑制自动 scm 触发 - 开发?

我试过文档 https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob。没什么好说的。好像不支持。

所以我想我唯一的方法是通过配置块将自定义属性直接添加到 XML。

我想要的:

    <strategy class="jenkins.branch.NamedExceptionsBranchPropertyStrategy">
      <defaultProperties class="java.util.Arrays$ArrayList">
        <a class="jenkins.branch.BranchProperty-array">
          <jenkins.branch.NoTriggerBranchProperty/>
        </a>
      </defaultProperties>
      <namedExceptions class="java.util.Arrays$ArrayList">
        <a class="jenkins.branch.NamedExceptionsBranchPropertyStrategy$Named-array">
          <jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named>
            <props class="empty-list"/>
            <name>development</name>
          </jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named>
        </a>
      </namedExceptions>
    </strategy>

我尝试过的:

multibranchPipelineJob(jobName) {    
    branchSources {
        git {
            remote(gitRepo)
            credentialsId(credentials)
            includes('*')
            configure {
             it / "sources  class='jenkins.branch.MultiBranchProject$BranchSourceList'" / 'data' / 'jenkins.branch.BranchSource' / "strategy class='jenkins.branch.DefaultBranchPropertyStrategy'" << name('development')               
            }        
        }           
    }
}

这很有用,但总是崩溃 http://job-dsl.herokuapp.com/ 这不是很有用https://github.com/jenkinsci/job-dsl-plugin/blob/master/docs/The-Configure-Block.md

我不知道自己在做什么,文档、手册和教程根本没有帮助。

编辑:

现在我有了这个。它有效,有点...

我能够生成作业,但是当我尝试重新保存作业时 Jenkins 抛出错误。输出 XML 有点不同。

multibranchPipelineJob(jobName) {

        configure { 
            it / sources(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource' << {
                source(class: 'jenkins.plugins.git.GitSCMSource') {
                  id(randomId)
                  remote(gitRepo)
                  credentialsId(credentials) 
                }
                strategy(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy") {
                    defaultProperties(class: "java.util.Arrays$ArrayList")  {
                        a(class: "jenkins.branch.BranchProperty-array") {
                            'jenkins.branch.NoTriggerBranchProperty'()
                        }
                    }
                    namedExceptions(class: "java.util.Arrays$ArrayList") {
                     a(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy$Named-array") {
                       'jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named'() {
                           props(class: "empty-list")
                           name('development') 
                       }
                   }        
                }

                }  

            } 
            } 

}

您可能已经注意到,它非常丑陋。希望以后有人修复这个插件。

您可以通过两种方式实现此目的

  • 按照 中的说明在 UI 中进行配置,您说您不想这样做。
  • 按照说明在 DSL 中使用条件语句 here

所以有效的代码如下:

    UUID uuid = UUID.randomUUID()
println('Random UUID: ' + uuid)

multibranchPipelineJob('test') {

  configure {
    it / sources / 'data' / 'jenkins.branch.BranchSource' << {
      source(class: 'jenkins.plugins.git.GitSCMSource') {
        id(uuid)
        remote('...')
        credentialsId('...')
        includes('*')
        excludes('')
        ignoreOnPushNotifications('false')
        traits {
            'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
        }
      }
      strategy(class: 'jenkins.branch.NamedExceptionsBranchPropertyStrategy') {
        defaultProperties(class: 'empty-list')
        namedExceptions(class: 'java.util.Arrays$ArrayList') {
          a(class: 'jenkins.branch.NamedExceptionsBranchPropertyStrategy$Named-array') {
            'jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named'() {
              props(class: 'java.util.Arrays$ArrayList') {
                a(class: 'jenkins.branch.BranchProperty-array') {
                  'jenkins.branch.NoTriggerBranchProperty'()
                }
              }
              name('master')
            }
          }
        }
      }
    }
  }
}