Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins JobDSL multibranchPipelineJob change script path

I am trying to create a multibranchPipelineJob in jobDSL, however the Jenkinsfile is at an alternative location to the default. I have looked through the docs https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob and I cannot see a way to do this. Looking at the config.xml for a manually created multibranchPipelineJob the scriptPath is in the section, but I cannot find a DSL method to set this.

Can anyone offer any help on how to do this? Cheers

like image 514
apr_1985 Avatar asked Jan 16 '18 15:01

apr_1985


3 Answers

Job DSL now exposes a way to do this:

multibranchPipelineJob('my-build') {
    factory {
        workflowBranchProjectFactory {
            scriptPath('path-to-Jenkinsfile')
        }
    }
}

Confirmed working with Job DSL 1.69, and is available since 1.67 according to the release notes.

Edit: Tested again with Job DSL 1.77 and it's still working as expected. If you want to see the documentation for this syntax you'll have to look at a Jenkins installation that has the Multibranch Pipeline plugin installed, at this path:

https://your-jenkins-url/plugin/job-dsl/api-viewer/index.html#path/multibranchPipelineJob-factory-workflowBranchProjectFactory-scriptPath

like image 129
nerdherd Avatar answered Oct 18 '22 23:10

nerdherd


As this question still proves popular, to do this in JCasC you can do this

jobs:
  - script: >
      folder('common');
      multibranchPipelineJob('common/jcasc-deploy') {
        factory {
          workflowBranchProjectFactory {
            scriptPath('Jenkinsfile')
          }
        }
        branchSources {
          branchSource {
            source {
              gitSCMSource {
                remote('[email protected]:PROJECT/REPO.git')
                credentialsId('gitlab-key')
                id('jcasc-deploy')
              }
            }
          buildStrategies {
            buildAllBranches {
              strategies {
                skipInitialBuildOnFirstBranchIndexing()
              }
            }
          }
        }
      }
      triggers {
        periodicFolderTrigger {
          interval('1440')
        }
      }
      configure { node ->
        node / sources / data / 'jenkins.branch.BranchSource' / source / traits {
          'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
        }
      }
    }
like image 8
apr_1985 Avatar answered Oct 19 '22 01:10

apr_1985


After a fair amount of googling, I found something that works:

configure {
    it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
        owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
        scriptPath("jenkins/[where ever you want]/Jenkinsfile")
    }
}

This seems to work for me.

like image 7
kgdesouz Avatar answered Oct 19 '22 01:10

kgdesouz