Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-49615

multibranchPipelineJob: add property strategy & script path options

      The job-dsl-plugin does not support the `Branch Sources > Property Strategy` and `Build Configuration > Script Path` options for multi-branch pipeline projects.

      It would useful to be able to specify these in the DSL similar to these examples:

      Apply Default Property Strategy to a Branch Source

      multibranchPipelineJob("my_awesome_job") {
          displayName "my awesome job"
          description "multi-branch pipeline job thingy"
          branchSources {
              git {   
                  remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                  credentialsId('github-ci')
                  includes('JENKINS-*')
                  // set the default property strategy
                  defaultBranchPropertyStrategy {
                      noTriggerBranchProperty()
                  }
              }
          }
      }
      

       

      Apply Named Branch Property Strategy to a Branch Source

      multibranchPipelineJob("my_awesome_job") {
          displayName "my awesome job"
          description "multi-branch pipeline job thingy"
          branchSources {
              git {   
                  remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                  credentialsId('github-ci')
                  includes('JENKINS-*')
                  // set the named branch property strategy
                  namedExceptionsBranchPropertyStrategy {
                      // only trigger branch build for the `master` branch
                      defaultProperties {
                          noTriggerBranchProperty()
                      }
                      namedException {
                          // could include noTriggerBranchProperty() here if there was no default properties
                          branch('master')
                      }
                  }
              }
          }
      }
      

      Set Script Path for the Project Factory

      multibranchPipelineJob("my_awesome_job") {
          displayName "my awesome job"
          description "multi-branch pipeline job thingy"
          branchSources {
              git {   
                  remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                  credentialsId('github-ci')
                  includes('JENKINS-*')
              }
          }
          factory {
              workflowBranchProjectFactory {
          	    scriptPath('pipelines/entryPoint.groovy')
              }
          }
      }
      

          [JENKINS-49615] multibranchPipelineJob: add property strategy & script path options

          matthew snoddy added a comment - PR Submitted: https://github.com/jenkinsci/job-dsl-plugin/pull/1109

          All these options (and many more) are already supported by the Dynamic DSL:

          multibranchPipelineJob('example-1') {
            branchSources {
              branchSource {
                source {
                  git {
                    id('3d9bee85-412a-47ab-b081-541981dde14a')
                    remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                    credentialsId('github-ci')
                  }
                }
                strategy {
                  defaultBranchPropertyStrategy {
                    props {
                      noTriggerBranchProperty()
                    }
                  }
                }
              }
            }
          }
          
          multibranchPipelineJob('example-2') {
            branchSources {
              branchSource {
                source {
                  git {
                    id('3d9bee85-412a-47ab-b081-541981dde14a')
                    remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                    credentialsId('github-ci')
                  }
                }
                strategy {
                  namedExceptionsBranchPropertyStrategy {          
                    defaultProperties {
                      noTriggerBranchProperty()
                    }
                    namedExceptions {
                      named {
                        name('master')
                      }
                    }
                  }
                }
              }
            }
          }
          
          multibranchPipelineJob('example-3') {
            branchSources {
              branchSource {
                source {
                  git {
                    id('3d9bee85-412a-47ab-b081-541981dde14a')
                    remote('https://github.com/jenkinsci/job-dsl-plugin.git')
                    credentialsId('github-ci')
                  }
                }      
              }
            }
            factory {
              workflowMultiBranchProjectFactory {
               scriptPath('pipelines/entryPoint.groovy')
              }
            } 
          }
          

          Daniel Spilker added a comment - All these options (and many more) are already supported by the Dynamic DSL : multibranchPipelineJob( 'example-1' ) { branchSources { branchSource { source { git { id( '3d9bee85-412a-47ab-b081-541981dde14a' ) remote( 'https: //github.com/jenkinsci/job-dsl-plugin.git' ) credentialsId( 'github-ci' ) } } strategy { defaultBranchPropertyStrategy { props { noTriggerBranchProperty() } } } } } } multibranchPipelineJob( 'example-2' ) { branchSources { branchSource { source { git { id( '3d9bee85-412a-47ab-b081-541981dde14a' ) remote( 'https: //github.com/jenkinsci/job-dsl-plugin.git' ) credentialsId( 'github-ci' ) } } strategy { namedExceptionsBranchPropertyStrategy { defaultProperties { noTriggerBranchProperty() } namedExceptions { named { name( 'master' ) } } } } } } } multibranchPipelineJob( 'example-3' ) { branchSources { branchSource { source { git { id( '3d9bee85-412a-47ab-b081-541981dde14a' ) remote( 'https: //github.com/jenkinsci/job-dsl-plugin.git' ) credentialsId( 'github-ci' ) } } } } factory { workflowMultiBranchProjectFactory { scriptPath( 'pipelines/entryPoint.groovy' ) } } }

          Ben Keil added a comment -

          Do you have an example how to add traits via Dynamic DSL?

          Ben Keil added a comment - Do you have an example how to add traits via Dynamic DSL?

          I tried the suggested approach of

            factory {
              workflowMultiBranchProjectFactory {
               scriptPath('pipelines/entryPoint.groovy')
              }
            } 
          

          However, it doesn't appear to do anything.
          My current workaround is the following:

          Closure addManualXmlOption(String JenkinsfilePath) {
              return {
                  it / 'factory' << 'scriptPath'(JenkinsfilePath)
                  // disable the default github commit status notifier since it's useless (always uses the same context)
                  // and all of the jobs do their own github status notification instead.
                  it / 'sources'/ 'data' / 'jenkins.branch.BranchSource'/ 'source'/ 'traits' << 'com.adobe.jenkins.disable__github__multibranch__status.DisableStatusUpdateTrait'(plugin:'disable-github-multibranch-status@1.1')
              }
          }
          
          multibranchPipelineJob("${i}-TEST-pipeline") {
              branchSources ....
              configure addManualXmlOption('path/to/Jenkinsfile')
          }
          

          Alex Richardson added a comment - I tried the suggested approach of factory { workflowMultiBranchProjectFactory { scriptPath( 'pipelines/entryPoint.groovy' ) } } However, it doesn't appear to do anything. My current workaround is the following: Closure addManualXmlOption( String JenkinsfilePath) { return { it / 'factory' << 'scriptPath' (JenkinsfilePath) // disable the default github commit status notifier since it's useless (always uses the same context) // and all of the jobs do their own github status notification instead. it / 'sources' / 'data' / 'jenkins.branch.BranchSource' / 'source' / 'traits' << 'com.adobe.jenkins.disable__github__multibranch__status.DisableStatusUpdateTrait' (plugin: 'disable-github-multibranch-status@1.1' ) } } multibranchPipelineJob( "${i}-TEST-pipeline" ) { branchSources .... configure addManualXmlOption( 'path/to/Jenkinsfile' ) }

          arichardson Here's the correct syntax for setting the path: https://stackoverflow.com/a/50775826/1222654

          Jordan Jennings added a comment - arichardson Here's the correct syntax for setting the path: https://stackoverflow.com/a/50775826/1222654

            daspilker Daniel Spilker
            djfdyuruiry matthew snoddy
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: