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

时间:2017-11-14 17:21:28

标签: jenkins jenkins-pipeline jenkins-job-dsl jenkins-groovy

如何添加默认的Suppress自动scm触发,除了命名分支 - 在Job DSL中开发?

我试过docs https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob。它并没有多说。好像它不受支持。

所以我想我唯一的办法是通过configure block直接向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') 
                       }
                   }        
                }

                }  

            } 
            } 

}

你可能已经注意到,它非常难看。希望将来有人会修复插件。

2 个答案:

答案 0 :(得分:3)

所以正在运行的代码如下:

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

答案 1 :(得分:0)

您可以通过两种方式实现这一目标

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