Multi branch Pipeline plugin load multiple jenkinsfile per branch

6.4k views Asked by At

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: '[email protected]: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?

2

There are 2 answers

0
user8096147 On

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'

}
2
hakamairi On

Most likely you want to map old Jenkins' jobs (pre pipeline) operating on single branch of some project to a single multi branch pipeline. The appropriate approach would be to create stages that are input dependent (like a question user if he/she wants to deploy to staging / live).

Alternatively you could just create a new separate Pipeline jenkins job that actually references an your project's SCM and points to your other Jenkinsfile (then one pipeline job per every other jenkinsfile).