28

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

apr_1985
  • 1,764
  • 2
  • 14
  • 27
  • 2
    Hmmm it appears that the ability to do this is in master https://github.com/jenkinsci/job-dsl-plugin/blob/master/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/jobs/MultibranchWorkflowJob.groovy So i might just have to wait for 1.67 of DSL to come out :( – apr_1985 Jan 16 '18 at 16:05

5 Answers5

35

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

nerdherd
  • 2,508
  • 2
  • 24
  • 40
  • 1
    This should be definitly added to the Jenkins Job Dsl documentation. – Matthias B May 24 '19 at 12:13
  • Can multiple scriptPath be added? – NickBroon Jun 04 '20 at 10:58
  • Strange could neither get it running on 1.77 / or the DSL playground. I get the following error: javaposse.jobdsl.dsl.DslScriptException: (script, line 3) No signature of method: javaposse.jobdsl.dsl.helpers.workflow.BranchProjectFactoryContext.workflowBranchProjectFactory() is applicable for argument types: (script$_run_closure1$_closure2$_closure3) values: [script$_run_closure1$_closure2$_closure3@33e4c96c] – Joerg S Dec 17 '20 at 09:29
  • doesn't seem to work on https://job-dsl.herokuapp.com/ it throws javaposse.jobdsl.dsl.DslScriptException: (script, line 3) No signature of method: javaposse.jobdsl.dsl.helpers.workflow.BranchProjectFactoryContext.workflowBranchProjectFactory() is applicable for argument types: (script$_run_closure1$_closure2$_closure3) values: [script$_run_closure1$_closure2$_closure3@ef99655] – Peter Kahn Jul 01 '22 at 19:23
8

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('git@gitlab.com: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'()
        }
      }
    }
apr_1985
  • 1,764
  • 2
  • 14
  • 27
  • 3
    This is useful but it didn't work for me initially until I realized that this requires the `basic-branch-build-strategies` plugin for the `buildStrategies` part to work. – Yeroc Jun 26 '20 at 17:50
  • @Yeroc thanks, your comment saved a lot working time – c4f4t0r Dec 14 '20 at 18:49
7

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.

kgdesouz
  • 1,966
  • 3
  • 16
  • 21
  • 1
    This is the solution for everybody where the accepted answer does not work, which currently seems to be the case for jobDSL 1.77. – Joerg S Dec 09 '20 at 16:53
  • @JoergS This answer should work for any version of Job DSL, but it's using a more "raw" syntax from before Job DSL supported the nicer syntax. Job DSL 1.67 introduced a nicer syntax which still works on 1.77 (I tested on a fresh Jenkins install with Job DSL 1.77 to be sure). – nerdherd Dec 10 '20 at 13:23
3

The setting is a bit hidden, but the Automatically Generated DSL supports setting the script path for a multibranch job:

multibranchPipelineJob('example') {
  factory {
    workflowMultiBranchProjectFactory {
      scriptPath('my-location/Jenkinsfile')
    }
  }
} 
daspilker
  • 8,154
  • 1
  • 35
  • 49
  • Although I can understand why it _should_ work, it didn't for me :-(. Have you tested it @daspilker ? – Steve-B Apr 23 '18 at 13:14
0

We found this approach to work on the dsl playground and when using a gradle JavaExec task in addition to the plugin. Found it here.


    configure {

        it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
            owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
            scriptPath("jenkinsfile")
        }
    }

Our Gradle Approach is as follows which lets engineers generate locally and we have jenkins generate/store with the build. This makes diffing against current easier.

sourceSets {
    main {
        groovy {
            srcDirs = ['src']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    test {
        groovy {
            srcDirs = ['test']
        }
        resources {
            srcDirs = ['resources']
        }
    }
    jobs {
        groovy {
            srcDirs 'jenkins-job-dsl'
            compileClasspath += main.compileClasspath
        }
    }
}

dependencies {
...
compile 'org.jenkins-ci.plugins:job-dsl-core:1.77'
}
...
/**
 * get current branch
 * @return uses vairable on jenkins and runs git command locally
 */
String getBranchName() {
    if (System.env.BRANCH_NAME) {
        // Jenkins
        if (System.env.BRANCH_NAME == 'master') {
            return ''
        } else {
            return '_' + System.env.BRANCH_NAME.toLowerCase().replaceAll("/", "_")
        }
    } else {
        return '_localDev'
    }
}

Peter Kahn
  • 12,364
  • 20
  • 77
  • 135