You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkins流水线中参数化传递Gatling的simulationClass

Jenkins流水线实现Gatling仿真类的参数化选择

方案1:基础参数化(字符串输入/固定下拉选择)

适合快速实现,无需额外插件:

  1. 配置流水线参数
    在Jenkins流水线的参数化构建中添加参数:

    • 若允许用户输入类名:添加字符串参数,命名为SIMULATION_CLASS,描述写“请输入仿真类全限定名(如com.example.simulations.MySimulation)”
    • 若想让用户直接选择(更友好):添加选择参数,命名为SIMULATION_CLASS,把所有已有的仿真类全限定名作为选项填入,描述写“选择要运行的仿真类”
  2. 修改Jenkinsfile脚本
    将静态的Gatling命令替换为参数化引用,同时增加参数校验防止空输入:

    pipeline {
        agent any
        parameters {
            // 二选一:字符串输入或选择参数
            string(name: 'SIMULATION_CLASS', defaultValue: '', description: '输入仿真类全限定名(例:com.example.simulations.MySimulation)')
            // choice(name: 'SIMULATION_CLASS', choices: ['com.example.simulations.MySimulation', 'com.example.simulations.OrderSimulation'], description: '选择仿真类')
        }
        stages {
            stage('执行Gatling仿真') {
                steps {
                    script {
                        if (!params.SIMULATION_CLASS) {
                            error('必须输入或选择要运行的仿真类!')
                        }
                        // 执行参数化命令
                        sh "mvn gatling:test -Dgatling.simulationClass=${params.SIMULATION_CLASS}"
                    }
                }
            }
        }
    }
    

方案2:动态下拉选择(自动识别仿真类)

如果仿真类会频繁新增,不想手动维护选项列表,可以用Active Choices插件实现动态下拉:

  1. 安装插件
    在Jenkins插件管理中安装Active Choices插件

  2. 配置动态参数
    在流水线参数中添加Active Choice Reactive Parameter,命名为SIMULATION_CLASS,脚本部分用Groovy扫描代码目录自动获取所有Gatling仿真类:

    pipeline {
        agent any
        parameters {
            activeChoiceReactiveParam('SIMULATION_CLASS') {
                description('选择要运行的Gatling仿真类')
                choiceType('SINGLE_SELECT')
                script {
                    def simulationClasses = []
                    // 扫描src/test/java下继承自Simulation的类
                    new File("${workspace}/src/test/java").eachFileRecurse(groovy.io.FileType.FILES) { file ->
                        if (file.name.endsWith('.java')) {
                            def content = file.text
                            if (content.contains('extends Simulation')) {
                                // 提取包名和类名,生成全限定名
                                def packageMatch = content =~ /package\s+([\w.]+);/
                                def className = file.name.replace('.java', '')
                                if (packageMatch.matches()) {
                                    simulationClasses.add("${packageMatch[0][1]}.${className}")
                                }
                            }
                        }
                    }
                    return simulationClasses ?: ['无可用仿真类']
                }
            }
        }
        stages {
            stage('执行Gatling仿真') {
                steps {
                    sh "mvn gatling:test -Dgatling.simulationClass=${params.SIMULATION_CLASS}"
                }
            }
        }
    }
    

这样用户在触发流水线时,只需输入或选择仿真类名称,无需手动修改命令,完全满足非技术用户的操作需求。

内容的提问来源于stack exchange,提问作者Imy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 08:05:24