Jenkins Multi-Branch Pipeline -- include another script into local Jenkinsfile

10,655

If you have common build logic across repos, you can move most of the pipeline logic to a separate groovy script. This script can then be referenced in any Jenkinsfile. This could be done either by checking another checkout of the repo that the the groovy script is in to another directory and then doing a standard groovy load or, probably the better approach would be by storing it as a groovy script in the Jenkins Global Script Library - which is essentially a self-contained git repo within Jenkins (see https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/README.md for more details).

We had a similar requirement, and created a global groovy method in a script that was maintained in Git and deployed to Jenkins' Global script library under /vars/ when it changed: e.g. the script 'scriptName.groovy' has

def someMethod(){
   //some build logic
   stage 'Some Stage'
   node(){
    //do something
   }
}

That way the common function could be called in any Jenkinsfile via

scriptName.methodName()
Share:
10,655
David W.
Author by

David W.

Updated on July 15, 2022

Comments

  • David W.
    David W. almost 2 years

    We are just starting out using Jenkins Multi-branch pipelines. I like the idea of Jenkins automatically creating a new Jenkins job when a new branch is created. It will make sure that all releasable development is being built in Jenkins. We have about 40 or 50 projects that get branched for almost every release, and creating those 40 or so jobs every time we branch is error prone work.

    However, I see there are two types of pipeline builds in Jenkins:

    • Regular Pipeline builds: You specify the location and branch in your Jenkins job. However, you can specify whether you want to use the script inside your Jenkins job configuration, or a script from your source repository. This would allow us to maintain a single Jenkinsfile for all of our jobs. If we change something in the build procedure, we only have to edit a single Jenkinsfile.

    • Multi-Branch Pipeline builds: Jenkins will automatically create a new Jenkins job for you when a new branch is created. This means we no longer have to create dozens of new Jenkins projects when a new branch occurs. However, it looks like the Jenkinsfile must be located on the root of the project. If you make a basic change in your build procedure, you have to update all Jenkins projects.

    I'd like to be able to use the Multi-branch Pipeline build, but I want to either specify where to pull up the Jenkinsfile from our repository, or include a master Jenkinsfile from a repository URL.

    Is there a way to do this with Jenkins Multi-branch pipelines?

  • rbellamy
    rbellamy about 7 years
    Yep, take a look at this answer for further examples.
  • 0neel
    0neel almost 7 years
    Also, if you give a method name "call" you'll be able to call it with script file name. So if you have a file /vars/scriptName.groovy you'll use it like scriptName()