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

喜欢而已 提交于 2019-12-07 06:04:35

问题


How do I add default Suppress automatic scm triggering except for named branch - development in Job DSL?

I tried docs https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob . It doesn't say much. Seems like it is not supported.

So I guess my only way is adding custom properties directly to XML via configure block.

What I want:

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

What I've tried:

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

This is useful, but keeps crashing http://job-dsl.herokuapp.com/ This is not so useful https://github.com/jenkinsci/job-dsl-plugin/blob/master/docs/The-Configure-Block.md

I have no idea what I'm doing and the docs, manuals and tutorials are not helpful at all.

EDIT:

Now I have this. It works, sort of...

I'm able to generate the job, but Jenkins throws an error when I try to resave the job. The output XML is somehow different.

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

                }  

            } 
            } 

}

As you might have noticed, it is extremely ugly. Hopefully someone will fix the plugin in the future.


回答1:


So the code which is working is the following:

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



回答2:


You can achieve this in two ways

  • Configuring in UI as explained here, which you said you do not want to do.
  • Using conditional statements in DSL as explained here


来源:https://stackoverflow.com/questions/47291748/adding-suppress-automatic-scm-triggering-except-for-named-branches-in-jenkins-jo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!