Search code examples
jenkinsgroovycustomizationshared

Custom initialization steps in Jenkins Shared Library


There's a Jenkins Shared Library containing a declarative pipeline definition which I intend to use in my project. It's available in this form:

// vars/sharedLibrary.groovy

def call(Map config = [:]) {
   pipeline {
       stages {
           // ...
       }
   }
}

I'm not the owner of the library code and don't really want (or can) change or fork it.

Now, using the library in my project would look like this:

// Jenkinsfile

sharedLibrary param1: 'value', param2: 'values'

The problem is that I need to execute a few custom initialization steps before sharedLibrary is run. I'm struggling to implement it since sharedLibrary declares the "full" pipeline with the pipeline {} block, not allowing me to inject any custom logic prior to that.

This is what I want (which is obviously incorrect):

// Jenkinsfile

pipeline {
    stages {
        stage('My custom initialization logic') {
            // ...
        }
    }

    // The rest of the shared logic goes here:
    sharedLibrary param1: 'value', param2: 'values'
}

What would be your advice on making this possible?


Solution

  • Use scrpted pipeline syntax instead.

    @Library('pipeline-sample')_
    
    node {
        echo 'Do your stuff here'
    }
    
    sharedLibrary param1: 'value', param2: 'values'