Jenkins template for all projects

For example:

I have 30 multi-faceted Git projects. All projects have their own Jenkins file. And suddenly I find the cool Jenkins plugin that I want to add for all projects. It is a pain to do this in all projects, and it is a big waste of time.

Is it possible to create some kind of Jenkinsfile template that will be something like a wrapper for the Jenkinsfile project or something like that gives me the ability to make changes in 1 place instead of 30 places?

I want something like this:

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

The template file that is in some repo looks like this. All projects have their own stages, which are defined in the middle of the Jenkinsfile template and defined in the project.

So, the Jenkins file in the project should:

  • download template from repository
  • put the steps in the middle of the Jenkinsfile template
+8
jenkins jenkins-pipeline
source share
2 answers

You can use:

+4
source share

As i always say

Friends do not give friends a template template.

Typically, my last use is to write code, which in turn generates more code. Also, this is not useful in your use case.

What you are really asking is, "I have a code that I want to split between 30 Jenkinsfiles." The best way to share code is, of course, to create a shared library and use that code.

Fortunately, this concept is fully supported by Jenkinsfiles . I highly recommend using it for your use case. From the introduction paragraph in the link above:

As the pipeline is adopted for an increasing number of projects in the organization, common patterns are likely to appear. It is often useful to share parts of pipelines between different projects to reduce layoffs and save the code “DRY”.

Pipeline supports the creation of “shared libraries” that can be defined in external source control repositories and loaded into existing pipelines.

Then just update the library and all the Jenkinsfiles that use it will also be updated. You can run a version of your shared library, and tuning will be much easier to debug.

+5
source share

All Articles