apr_1985
apr_1985

Reputation: 1962

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

Upvotes: 27

Views: 17727

Answers (5)

Peter Kahn
Peter Kahn

Reputation: 13026

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'
    }
}

Upvotes: 0

nerdherd
nerdherd

Reputation: 2603

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

Upvotes: 35

apr_1985
apr_1985

Reputation: 1962

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'()
        }
      }
    }

Upvotes: 8

kgdesouz
kgdesouz

Reputation: 1996

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.

Upvotes: 7

daspilker
daspilker

Reputation: 8194

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')
    }
  }
} 

Upvotes: 3

Related Questions