Search code examples
gitjenkinsjenkins-pipeline

Reuse Jenkinsfile in different projects


The scenario:

We have a bunch of projects, each lying in it's own git repository. I want to build them with Jenkins 2 multibranch pipeline jobs. One job for every repository. The Jenkinsfile for every project looks the same and changes made to the build process need to be rolled out to every project. These changes can be small fixes in a build step or adding a complete new build step.

The idea:

To keep maintainabillity as high as possible i would like to use just one Jenkinsfile and include it in every project, so i only have to modify this single Jenkinsfile to modify all projects build workflow.

The question

Is it possible (and how) to achieve this goal. Deploy one single Jenkinsfile to a bunch of repository so that i only have to change one file to change all build jobs?

additional

I know that there is the possibility to use shared libraries, where i could define the execution code of the build steps. But if want to add a complete new build step i still would have to modify all jenkinsfiles which i don't want to do. So I am really looking for a way to share the whole Jenkinsfile for all projects, if possible.

Tried so far

  1. I thought to use the brand new feature to be able to tell Jenkins where the jenkinsfile is located and use a submodule to include it in every project. But that does not seem to work, as Jenkins seems to look for the Jenkinsfile on the git-Server where it will never show up as it is a submodule...

Solution

  • It is not only possible to put pipeline steps into a shared library. It is also possible to put your complete pipeline as global variable into a shared library. With this approach your Jenkinsfile in each repository needs only one line of code then. And you can modify the pipeline of all projects in one single place (in the shared library).