aRTURIUS
aRTURIUS

Reputation: 1370

Use Jenkins shared lib in parameters step

trying to apply jenkins shared library to use in pipelines and want to add some choice parameters to don't update all pipelines with new values for example, created a class with static method:

#!/usr/bin/env groovy

class Envs implements Serializable {

  static giveMeParameters (script) {
    return [
      script.string(defaultValue: '', description: 'A default parameter', name: 'textParm')
    ]
  }
}

and trying to use it in pipeline: pipeline {

parameters {

    string(name: 'ENV', defaultValue: 'staging', description: 'Please enter desire env name for app', trim: true)
    Envs.giveMeParameters (this)
}

but getting error:

WorkflowScript: 81: Invalid parameter type "giveMeParameters". Valid parameter types: [booleanParam, buildSelector, choice, credentials, file, gitParameter, text, password, run, string] @ line 81, column 9.
           giveMeParameters (this)

Upvotes: 0

Views: 736

Answers (1)

yong
yong

Reputation: 13712

Your giveMeParameters() return a params Array, but parameters {} not accept an Array.

To combine the params return by share lib and params of project, you can try as following.

projectParams = [
   // define project params here
   booleanParam(name: 'param1', defaultValue: false,
      description: ''
   ),
   string(name: 'param2', description: ''),
]

// extend common params return by share lib to projectParams
projectParams.addAll(giveMeParameters())

properties([parameters(projectParams)])

// you can choose put above lines either outside the pipeline{} block
// or put in a stage's script step

pipeline {
   
  stages {
    stage('Init') {
      steps {
        // you can choose put outside pipeline {}
        script {
          projectParams = [
             // define project params here
             booleanParam(name: 'param1', defaultValue: false,
                description: ''
             ),
             string(name: 'param2', description: ''),
          ]

          // extend common params return by share lib to projectParams
          projectParams.addAll(giveMeParameters())

          properties([parameters(projectParams)])          
        }
      }
    }
  }
}

Upvotes: 1

Related Questions