是否可以在 Jenkins 参数名称中包含空格?
Is it possible to have a Jenkins parameter's name with spaces in it?
我正在努力让我的 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”。
在这个 example 中,我看到此人确实使用名称中带有空格的“BooleanParameterDefinition”。但我似乎无法弄清楚如何在名称中使用空格。 名字中有空格是我在这里的唯一目标。
在Java和Groovy中space不支持变量!不推荐,但 Jenkins 支持 'String referencing'
但是如果你想装饰参数显示名称它会是这样的
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}"
}
}
确实有可能,用户“字符串引用”可以访问它,即 params."Activate in Test"
例如:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
是的,有可能,只需使用以下符号:
${params['Name with space']}
在旧 Jenkins 上测试:2.149
我正在努力让我的 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”。
在这个 example 中,我看到此人确实使用名称中带有空格的“BooleanParameterDefinition”。但我似乎无法弄清楚如何在名称中使用空格。 名字中有空格是我在这里的唯一目标。
在Java和Groovy中space不支持变量!不推荐,但 Jenkins 支持 'String referencing' 但是如果你想装饰参数显示名称它会是这样的
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}"
}
}
确实有可能,用户“字符串引用”可以访问它,即 params."Activate in Test"
例如:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
是的,有可能,只需使用以下符号:
${params['Name with space']}
在旧 Jenkins 上测试:2.149