<p>The built-in tool that Jenkins provides for this situation is called <a href="https://www.jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">shared libraries</a>.  You can define common Pipeline steps within a shared library and then include those steps from within your Pipelines.  Just to lend some anecdotal support - since there are so many tasks in common across my team's build pipelines, the vast majority of our Pipeline code actually lives in shared libraries; the individual Jenkinsfiles in our repos mainly just call steps from the shared libraries.</p>

CC BY-SA 4.0
2021-07-02T18:08:31.347Z
2021-07-02T18:08:31.347Z
jayhendren
<p>I have the following case:</p>
<p>There are a couple of repositories (well, two-digit number). Each of them has some sort of (declarative) Jenkins pipeline - it would be one of three types:</p>
<ul>
<li>run tests -&gt; build docker image -&gt; deploy to test environment;</li>
<li>run tests -&gt; create package -&gt; upload to package repository (i.e. PyPI);</li>
</ul>
<p>Now, currently, each repository has a Jenkinsfile of one of the above types, and a Jenkinsfile which runs only the tests and does nothing more. These Jenkinsfiles are basically the same across the repositories - same code, same environment variables. The only thing that changes are the image/package names (and, of course, there are differences between building images and creating packages). If I have to make a change to any one Jenkinsfile, I have to make a change to all of them, go through Pull Requests etc., which is obviously too much unneeded work.</p>
<p>Not only this, but the project pipeline involves adding even more tasks performed by Jenkins - like running integration tests, deploying to higher environments, etc. Most of these integrations would require certain combinations of steps - i.e. today I want to build an image and deploy to environment A; tomorrow I want unit-test, deploy to environment B and integration-test; etc. As you see, these pipelines would have many steps in common. Following the current practice, this will lead to code duplication and maintenance costs.</p>
<p>If all this is implemented as separate Jenkinsfile for each pipeline, this will lead to explosion of Jenkinsfiles which are almost the same within a repository and across repositories.</p>
<p>What I want to do is to abstract away the common steps (like test a repository; build an image; create a package; run some integration tests; deploy to an environment) in a separate repository which will hold all such CI/CD code. Then I want to create <em>parameterized</em> (Jenkins) pipelines in each of the repositories, which just run these generic steps in any combination I wish with the configuration I have given for a concrete repository.</p>
<p>Three options I have researched:</p>
<ul>
<li>create a separate Jenkins job with a corresponding Jenkinsfile for each generic step; have a pipeline in each repository that just calls a combination of these generic steps providing parameters. This has the overhead of having to manually specify git-clone operations for the repository which needs to be built/tested in each generic step. <a href="https://devops.stackexchange.com/questions/6591/one-jenkinsfile-or-multiple">This answer</a> seems to point in this direction, but I am still concerned with the possible overhead.</li>
<li>call a combination the generic Jenkinsfiles from within a pipeline providing the necessary parameters. This has the overhead of rather low maintainability (calling Jenkinsfile from Jenkinsfile doesn't seem that straightforward and includes more lines of code).</li>
<li>call abstracted-away shell scripts. As I see it, this leads to even harder maintenance and even lower abstraction. It constrains the user to script everything in shell; loses all possible Jenkins plugins and other tools. While possible, it seems to me that it won't provide the level of abstraction I hope to achieve. Please correct me if I am wrong.</li>
</ul>
<p>Now the question is: how can I achieve this using (declarative) Jenkins pipelines with the least amount of overhead?</p>
<p>Even more importantly - are there tools that are better-suited for this kind of work?</p>

<jenkins><cicd><build-pipeline><toolchain>
Modularizing CI/CD pipelines in Jenkins or in other tools?
cyau
# Modularizing CI/CD pipelines in Jenkins or in other tools?

(Asked by: username_0 on 2021-07-01)

I have the following case:

There are a couple of repositories (well, two-digit number). Each of them has some sort of (declarative) Jenkins pipeline - it would be one of three types:

* run tests -> build docker image -> deploy to test environment;
* run tests -> create package -> upload to package repository (i.e. PyPI);

Now, currently, each repository has a Jenkinsfile of one of the above types, and a Jenkinsfile which runs only the tests and does nothing more. These Jenkinsfiles are basically the same across the repositories - same code, same environment variables. The only thing that changes are the image/package names (and, of course, there are differences between building images and creating packages). If I have to make a change to any one Jenkinsfile, I have to make a change to all of them, go through Pull Requests etc., which is obviously too much unneeded work.

Not only this, but the project pipeline involves adding even more tasks performed by Jenkins - like running integration tests, deploying to higher environments, etc. Most of these integrations would require certain combinations of steps - i.e. today I want to build an image and deploy to environment A; tomorrow I want unit-test, deploy to environment B and integration-test; etc. As you see, these pipelines would have many steps in common. Following the current practice, this will lead to code duplication and maintenance costs.

If all this is implemented as separate Jenkinsfile for each pipeline, this will lead to explosion of Jenkinsfiles which are almost the same within a repository and across repositories.

What I want to do is to abstract away the common steps (like test a repository; build an image; create a package; run some integration tests; deploy to an environment) in a separate repository which will hold all such CI/CD code. Then I want to create *parameterized* (Jenkins) pipelines in each of the repositories, which just run these generic steps in any combination I wish with the configuration I have given for a concrete repository.

Three options I have researched:

* create a separate Jenkins job with a corresponding Jenkinsfile for each generic step; have a pipeline in each repository that just calls a combination of these generic steps providing parameters. This has the overhead of having to manually specify git-clone operations for the repository which needs to be built/tested in each generic step. [This answer](https://devops.stackexchange.com/questions/6591/one-jenkinsfile-or-multiple) seems to point in this direction, but I am still concerned with the possible overhead.
* call a combination the generic Jenkinsfiles from within a pipeline providing the necessary parameters. This has the overhead of rather low maintainability (calling Jenkinsfile from Jenkinsfile doesn't seem that straightforward and includes more lines of code).
* call abstracted-away shell scripts. As I see it, this leads to even harder maintenance and even lower abstraction. It constrains the user to script everything in shell; loses all possible Jenkins plugins and other tools. While possible, it seems to me that it won't provide the level of abstraction I hope to achieve. Please correct me if I am wrong.

Now the question is: how can I achieve this using (declarative) Jenkins pipelines with the least amount of overhead?

Even more importantly - are there tools that are better-suited for this kind of work?

**Tags:** jenkins, cicd, build-pipeline, toolchain


---

## 1 answers

### Answer by username_1 on 2021-07-02. Score: 1 | Accepted answer

The built-in tool that Jenkins provides for this situation is called [shared libraries](https://www.jenkins.io/doc/book/pipeline/shared-libraries/). You can define common Pipeline steps within a shared library and then include those steps from within your Pipelines. Just to lend some anecdotal support - since there are so many tasks in common across my team's build pipelines, the vast majority of our Pipeline code actually lives in shared libraries; the individual Jenkinsfiles in our repos mainly just call steps from the shared libraries.


---

