Adding Suppress automatic scm triggering except fo

2019-06-27 21:14发布

问题:

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