Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Jenkinsfile for all project

For example:

I have 30 multibranch Git projects. All projects have their own Jenkinsfile. And suddenly I find a cool Jenkins plugin which I want to add for all projects. This is pain to do it on all projects and this is a big waste of time.

Is it possible to create somethink like template Jenkinsfile which will be something like wrapper for project Jenkinsfile or somethink like that which gives me possibility to do changes in 1 place instead of 30 places?

What I want is something like that:

stage {
 ...}
timestamps {
    <include rest of stages definied in projects>
}
stage {
... }

Template file which is in some repo is looked for like this. All of the projects have their own stages defined which are included in the middle of the template Jenkinsfile and defined in project.

So Jenkinsfile in project must:

  • load template from repository
  • put stages in the middle of template Jenkinsfile
like image 554
Kapitalny Avatar asked Feb 22 '17 15:02

Kapitalny


1 Answers

You could use:

  • the Config File Provider Plugin using the configuration files in Jenkins Pipelines:

    node {
      ...
      configFileProvider(
        [configFile(fileId: 'jenkinsfile-template', ...)]) {
          ...
        }
      ...
    }
    
  • load: Evaluate a Groovy source file into the Pipeline script:

    Takes a filename in the workspace and runs it as Groovy source text.

  • Shared Libraries:

    Oftentimes it is useful to share parts of Pipelines between various projects to reduce redundancies and keep code "DRY".

    Pipeline has support for creating "Shared Libraries" which can be defined in external source control repositories and loaded into existing Pipelines.

like image 195
Gerold Broser Avatar answered Oct 02 '22 06:10

Gerold Broser