如何将参数值映射到不同的值,然后在管道内执行。
parameters {
choice(name: 'SIMULATION_ID',
choices: 'GatlingDemoblazenFrontlineSampleBasicnGatlingDemoStoreNormalLoadTest',
description: 'Simulations')
}
如何将"GatlingDemobblaze"的值映射到"438740439023874",使其成为进入${params.SIMULATION_ID}的后者?我们能用一个简单的groovy代码做到这一点吗?
gatlingFrontLineLauncherStep credentialId: '', simulationId:"${params.SIMULATION_ID}"
谢谢你的帮助。
正如评论中所建议的,最好的方法是使用可扩展选择参数插件并定义所需的键值对,但是,如果你不想使用插件,你可以在管道脚本中使用groovy创建映射并使用它。
为此,你有几个选项:
如果你在单个阶段需要它,你可以定义script
块内的映射并在该阶段使用它:
pipeline {
agent any
parameters {
choice(name: 'SIMULATION_ID', description: 'Simulations',
choices: ['GatlingDemoblaze', 'FrontlineSampleBasic', 'GatlingDemoStoreNormalLoadTest'])
}
stages {
stage('Build') {
steps {
script {
def mappings = ['GatlingDemoblaze': '111', 'FrontlineSampleBasic': '222', 'GatlingDemoStoreNormalLoadTest': '333']
gatlingFrontLineLauncherStep credentialId: '', simulationId: mappings[params.SIMULATION_ID]
}
}
}
}
}
您也可以将其定义为在所有阶段都可用的全局参数(然后您不需要script
指令(:
mappings = ['GatlingDemoblaze': '111', 'FrontlineSampleBasic': '222', 'GatlingDemoStoreNormalLoadTest': '333']
pipeline {
agent any
parameters {
choice(name: 'SIMULATION_ID', description: 'Simulations',
choices: ['GatlingDemoblaze', 'FrontlineSampleBasic', 'GatlingDemoStoreNormalLoadTest'])
}
stages {
stage('Build') {
steps {
gatlingFrontLineLauncherStep credentialId: '', simulationId: mappings[params.SIMULATION_ID]
}
}
}
}
另一个选项是在environment
指令中设置这些值:
pipeline {
agent any
parameters {
choice(name: 'SIMULATION_ID', description: 'Simulations',
choices: ['GatlingDemoblaze', 'FrontlineSampleBasic', 'GatlingDemoStoreNormalLoadTest'])
}
environment{
GatlingDemoblaze = '111'
FrontlineSampleBasic = '222'
GatlingDemoStoreNormalLoadTest = '333'
}
stages {
stage('Build') {
steps {
gatlingFrontLineLauncherStep credentialId: '', simulationId: env[params.SIMULATION_ID]
}
}
}
}