1

I'm looking to see if it's possible for something like this: How can I define additional parameters in jenkinsfile who inherit from a pipeline shared lib?

But in a declarative pipeline, I've tried solutions similar to that of the post above but with no luck.

I need to be able to declare a shared library of build parameters which I can then use in multiple declared pipelines.

Something like this:

pipeline {  
agent {
    label 'slave'
}

parameters { // Build parameters
    string(defaultValue: 'test', description: 'SCM branch', name: 'UUT_BRANCH', trim: false)
    # DEFININED IN SHARED LIBRARY 
}

I wondered if anyone could provide any input? Many thanks.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
zstring
  • 27
  • 5

2 Answers2

0

I never used parameters which are inherited from a shared lib. But this is how it works with a function declared in the library:

Inside the library there is a groovy file containing the function:

def call(String name = 'human') {
    echo "Hello, ${name}!"
}

After configuring the library as a shared lib in jenkins, you can use the function in the declarative pipeline like this:

stage('useSharedLib'){
    steps {
        sayHello 'Stranger'
      }
    }

Maybe this will help you with implementing env vars

alex
  • 180
  • 1
  • 13
0

This is how I have solved mine need for shared parameter defined in library while individual jobs define their own custom parameters. This is how mine Jenkinsfile looks like:

#!groovy


@Library('my-library@master') _


properties([
    parameters([
        string(name: 'PARAM_A', defaultValue: '1', description: 'Aaa'),
        string(name: 'PARAM_B', defaultValue: '2', description: 'Bbb'),
        string(name: 'PARAM_C', defaultValue: '3', description: 'Ccc'),
    ] + runTest.commonJobParams())
])


runTest(
    params: params,
)

and this is how vars/runTest.groovy in my library looks like:

def commonJobParams() {
    return [
        string(
            name: 'GOLDEN',
            defaultValue: '999',
            description: 'Description of param from library',
        ),
    ]
}

def call(Map config) {
    pipeline {
        agent {
            ...
        }
        stages {
            stage('Test') {
                steps {
                    echo "Hello ${PARAM_A}"
                    echo "Hello ${PARAM_B}"
                    echo "Hello ${PARAM_C}"
                    echo "Hello ${GOLDEN}"
                }
            }
        }
    }
}
jhutar
  • 1,369
  • 2
  • 17
  • 32