在参数步骤中使用Jenkins共享库

ca1c2owp  于 2022-09-20  发布在  Jenkins
关注(0)|答案(1)|浏览(208)

尝试应用Jenkins共享库在管道中使用,并希望添加一些CHOICE参数以不使用新值更新所有管道例如,使用静态方法创建了一个类:


# !/usr/bin/env groovy

class Envs implements Serializable {

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

并试图在管道中使用它:管道{

parameters {

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

但得到了错误:

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)
hwamh0ep

hwamh0ep1#

您的giveMeParameters()返回参数数组,但parameters {}不接受数组。

要将共享库返回的参数和项目的参数组合在一起,可以尝试如下操作。

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)])          
        }
      }
    }
  }
}

相关问题