Pipelines in Jenkins

A few steps are required to create a pipeline in Groovy.

It’s a good practice to define the main flow in a library in order to share the functions and the steps across multiple projects.

Create the library

Create a project on git and push the definition of the pipelines.

I won’t explain here how pipelines work.

def call(foo) {
  pipeline {
    agent any
    stages {
      stage {
        steps {
          ...
        }
      }
    }
  }
}

Define the library in Jenkins

Go in Manage Jenkins, then Configure System. Scroll down to Global Pipeline Libraries.

Give a name to the library, specify the repository, and the branch.

Add the Jenkins build in the project

The project must have a script that imports the library and calls the functions of the library.

For example, if the library has been defined in jenkins as myscripts, the script must import the library myscripts and call its functions. It will be something like this

@Library('myscripts')

def foo = 'bar'

myPipeline(foo)

Configure the pipeline in the Jenkins’ build

Open the configuration of the project’s build and scroll down to Pipeline to configure the branch to build.

Then specify the entry point with the script in the project