我创建了一个共享库jobParams.groovy
,内容如下:
List commonParams() {
//return list of parameters
def paramsList = [
string(defaultValue: "Jenkins", description: 'Some Name', name: 'NAME'),
string(defaultValue: "no_title", description: 'Title', name: 'TITLE')
]
return paramsList
}
在作业管道脚本中,我将该列表读入另一个列表,但是不确定如何从列表中访问所需的值,例如,如何打印commonParams.NAME?
我在工作中有此内容,如管道脚本:
@Library('jenkins-library@master') _
List commonParams = jobParams.commonParams()
pipeline {
agent { label 'my-server' }
stages {
stage('params') {
steps {
println "commonParams"
}
}
}
此打印
[Pipeline] echo
[@string(defaultValue=Jenkins,description=Some Name,name=NAME), @string(defaultValue=no_title,description=Title,name=TITLE)]
[Pipeline] }
以下仅打印名称无效。
println "${commonParams.NAME}"
答案 0 :(得分:0)
您可以使用静态方法创建类
// file: /src/com/company/jenkins/JobParam.groovy
package com.company.jenkins
class JobParam {
static List commonParams() {
//return list of parameters
def paramsList = [
string(defaultValue: "Jenkins", description: 'Some Name', name: 'NAME'),
string(defaultValue: "no_title", description: 'Title', name: 'TITLE')
]
paramsList
}
}
来自管道(或/ var /文件夹):
@Library('jenkins-library') _
import com.company.jenkins.JobParam
List commonParams = JobParam.commonParams()
// your code