【问题标题】:Change the parameter values in a Jenkins Pipeline更改 Jenkins 流水线中的参数值
【发布时间】:2021-07-22 10:34:53
【问题描述】:

如何将参数值映射到不同的值,然后在管道内执行。

parameters {
      choice(name: 'SIMULATION_ID', 
        choices: 'GatlingDemoblaze\nFrontlineSampleBasic\nGatlingDemoStoreNormalLoadTest', 
        description: 'Simulations')
    }

如何将“GatlingDemoblaze”的值映射到“438740439023874”,以便后者进入 ${params.SIMULATION_ID}?我们可以用一个简单的 groovy 代码来做到这一点吗?

gatlingFrontLineLauncherStep credentialId: '', simulationId:"${params.SIMULATION_ID}"

感谢您的帮助。

【问题讨论】:

标签: jenkins-pipeline jenkins-declarative-pipeline


【解决方案1】:

正如 cmets 中所建议的,最好的方法是使用 Extensible Choice Parameter Plugin 并定义所需的键值对,但是如果您不想使用该插件,您可以在管道脚本中使用 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]
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多