Centralise Jenkins Pipelines configuration using Shared Libraries
Tags: cicd, jenkinspipelines, continuousdelivery, sharedlibraries
Published: 2017-05-15 16:06:59

---
title: Centralise Jenkins Pipelines configuration using Shared Libraries
published: true
description: Tutorial about how to keep your pipeline configuration centralised and share it to all your projects
tags: cicd, jenkins-pipelines, continuous-delivery, shared-libraries
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jswl8cnnqqejh0setg0o.png
---

This post is a continuation of the tutorial [Setup Continuos Integration/Delivery system in just 4 steps with Jenkins Pipelines and Blue Ocean](https://dev.to/jalogut/setup-continuos-integrationdelivery-system-in-just-4-steps-with-jenkins-pipelines-and-blue-ocean).

Now that you know how to setup your pipelines, you might want to go one step further and keep just one pipeline configuration for all your projects. That is possible using [Shared Libraries](https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-a-more-structured-dsl). The idea is that your standard pipeline configuration resides in a shared repository that will be accessed for each of your projects. Each project will then only set specific properties into its own `Jenkinsfile`.

Let’s see how to do it:

### Shared Library Repository

1- Create a new `git` repository where your `shared library` will reside.
2- Create a `vars` directory and a `.groovy` file inside. Call this file `standardPipeline.groovy` for example.
3-  Add the following code example into this file.

```groovy
    def call(body) {
    
        def config = [:]
        body.resolveStrategy = Closure.DELEGATE_FIRST
        body.delegate = config
        body()

        node {
            // Clean workspace before doing anything
            deleteDir()

            try {
                stage ('Clone') {
                    checkout scm
                }
                stage ('Build') {
                    sh "echo 'building ${config.projectName} ...'"
                }
                stage ('Tests') {
                    parallel 'static': {
                        sh "echo 'shell scripts to run static tests...'"
                    },
                    'unit': {
                        sh "echo 'shell scripts to run unit tests...'"
                    },
                    'integration': {
                        sh "echo 'shell scripts to run integration tests...'"
                    }
                }
      	        stage ('Deploy') {
                    sh "echo 'deploying to server ${config.serverDomain}...'"
    	        }
            } catch (err) {
                currentBuild.result = 'FAILED'
                throw err
            }
        }
    }
```
4- Commit and push

### Jenkins configuration

1- Go to `Manage Jenkins > Configure System > Global Pipeline Libraries` 
2- Give a name to your library and set the git url
![Global Pipeline Configuration](https://thepracticaldev.s3.amazonaws.com/i/svog5t4qkym153doewl3.png)


### Use library in your Project

1- Go to your project repository root and edit the content of your `Jenkinsfile` with just the following

```
    @Library("your-library-name") _
    standardPipeline {
        projectName = "Project1"
        serverDomain = "Project1 Server Domain"
    }
```

2-  Note that the name of the library must be the same as you previously set in Jenkins.

That's it! You can now check in Blue Ocean how your projects are using the shared pipeline configuration
![shared-library pipeline](https://thepracticaldev.s3.amazonaws.com/i/ledioxgmvxicggym8zu3.png)


### Review

Before we finish, let's see more in detail the code we used:

```groovy
@library("your-library-name") _
```

Here is where you define which library to import. You can use as many libraries as you want into the same `Jenkinsfile`. Do not forget the `_` at the end, it is needed!

```groovy
standardPipeline {
    //...
}
```

The name of this block has to match with the name of your `.groovy` library name. In this example that would be `standardBuild` because our file was called `standardPipeline.groovy`

```groovy
standardPipeline {
    projectName = "Project1"
    serverDomain = "Project1 Server Domain"
}
```

`projectName` and `serverDomain` are an example of how to set properties that can be accessed on the shared library:

```groovy
def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    //...
```

This code is responsible to load the properties previously mentioned. Thanks to that, properties can be accessed using `config.propertyName`

You can use properties to identify project settings or to execute different actions. For example:

```groovy
if (config.propertyName == true) {
    // Do something here   
}
```

That's all. Now you can play around and configure your shared pipelines. We use 2 pipelines configurations:

* `projectBuild.groovy` for our projects 
* `commonModuleBuild.groovy` to build common modules that can be installed as project dependencies. 

`commonModuleBuild.groovy` is a bit different as it does not need a `Deploy` step for example.

You can find the resources used for this tutorial here:

* [Shared Libraries Documentation](https://jenkins.io/doc/book/pipeline/shared-libraries/)
* [jalogut/jenkins-basic-shared-library-sample](https://github.com/jalogut/jenkins-basic-shared-library-sample)
* [jalogut/jenkinsfile-shared-library-sample](https://github.com/jalogut/jenkinsfile-shared-library-sample)