Jenkins – Multi branch Pipeline plugin load multiple jenkinsfile per branch

Jenkinsjenkins-pipeline

I am able to load Jenkinsfile automatically through multi branch pipeline plugin with a limitation of only one jenkinsfile per branch.

I have multiple Jenkinsfiles per branch which I want to load, I have tried with below method by creating master Jenkins file and loading specific files. In below code it merges 1.Jenkinsfile and 2.Jenkinsfile as one pipeline.

node {
  git url: 'git@bitbucket.org:xxxxxxxxx/pipeline.git', branch: 'B1P1'
      sh "ls -latr"
          load '1.Jenkinsfile'
              load '2.Jenkinsfile'

}

Is there a way I can load multiple Jenkins pipeline code separately from one branch?

Best Answer

I did this writing a share library (ref https://jenkins.io/doc/book/pipeline/shared-libraries/) containing the following file (in vars/generateJobsForJenkinsfiles.groovy):

/**
 * Creates jenkins pipeline jobs from pipeline script files
 * @param gitRepoName name of github repo, e.g. <organisation>/<repository>
 * @param filepattern ant style pattern for pipeline script files for which we want to create jobs
 * @param jobPath closure of type (relativePathToPipelineScript -> jobPath) where jobPath is a string of formated as '<foldername>/../<jobname>' (i.e. jenkins job path)
 */
def call(String gitRepoName, String filepattern, def jobPath) {
    def pipelineJobs = []
    def base = env.WORKSPACE
    def pipelineFiles = new FileNameFinder().getFileNames(base, filepattern)

    for (pipelineFil in pipelineFiles) {

        def relativeScriptPath = (pipelineFil - base).substring(1)
        def _jobPath = jobPath(relativeScriptPath).split('/')
        def jobfolderpath = _jobPath[0..-2]
        def jobname = _jobPath[-1]

        echo "Create jenkins job ${jobfolderpath.join('/')}:${jobname} for $pipelineFil"

        def dslScript = []
        //create folders
        for (i=0;  i<jobfolderpath.size(); i++)
            dslScript << "folder('${jobfolderpath[0..i].join('/')}')"

        //create job
        dslScript << """
            pipelineJob('${jobfolderpath.join('/')}/${jobname}') {
                definition {
                    cpsScm {
                        scm {
                            git {
                                remote {
                                    github('$gitRepoName', 'https')
                                    credentials('github-credentials')
                                }
                                branch('master')
                            }
                        }
                        scriptPath("$relativeScriptPath")
                    }
                }
                configure { d ->
                    d / definition / lightweight(true)
                }
            }
            """
        pipelineJobs << dslScript.join('\n')
        //println dslScript
    }

    if (!pipelineJobs.empty)
        jobDsl sandbox: true, scriptText: pipelineJobs.join('\n'), removedJobAction: 'DELETE', removedViewAction: 'DELETE'

}