我正在努力让我的 Jenkins UI 更加干净。
我的 Jenkins 文件调用一个函数,该函数依次运行以下内容:
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'GitlabConnection'],
[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'activateInTest'],
[$class: 'ChoiceParameterDefinition', choices: 'false\ntrue\n', description: 'If running newBuild, skip unit tests', name: 'skipUnitTests']
]]
])
目前,我可以这样访问这些参数:
if(activateInTest == 'true') {
//Do something
}
在阅读其他文档和示例之后。看起来好像我也可以通过执行类似
params.activateInTest
之类的操作来访问参数,但这不起作用。我也尝试过做类似params["activateInTest"]
的事情,但这也不起作用。我想以这种方式访问它的原因
params["..."]
,是因为我希望参数的名称为“Activate in Test”而不是“activateInTest”。在这个示例中,我看到这个人确实使用了名称中带有空格的“BooleanParameterDefinition”。但我似乎不知道如何在名称中使用空格。 名称中包含空格是我唯一的目标。
是的,有可能,只需使用以下符号:
${params['Name with space']}
在旧詹金斯上测试:2.149
确实可以,用户“字符串引用”来访问它,即
params."Activate in Test"
例如:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
如果你想装饰参数显示名称,它会是这样的
Jenkins 声明式管道
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
脚本化管道
node {
properties(
[
parameters(
[string(defaultValue: '/data', name: 'Directory', description: "Directort Path"),
string(defaultValue: 'Dev', name: 'DEPLOY_ENV', description: "Deploy Environment")
]
)
]
)
stage('debug') {
echo "${params}"
}
}