Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 292
bartimar Avatar asked Nov 14 '17 17:11

bartimar


People also ask

What is suppress automatic SCM triggering?

There should be an option to create branch projects automatically wherever a Jenkinsfile is detected, yet suppress the automatic SCM trigger which fires when branch indexing detects a new branch head. Manual or other automatic scheduled builds should still be possible.

Which option you will select to prevent a build from being started as soon as Jenkins discovers the first commit?

Click "Add property", choose "Suppress automatic SCM triggering" Save.


1 Answers

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')
            }
          }
        }
      }
    }
  }
}
like image 90
Indigo Ping Avatar answered Sep 17 '22 01:09

Indigo Ping