【问题标题】:Jenkins Groovy : Pass values(list) to Active Choice Parameter while calling different Jenkins jobJenkins Groovy:在调用不同的 Jenkins 作业时将值(列表)传递给 Active Choice 参数
【发布时间】:2021-09-22 08:06:26
【问题描述】:

我需要在从我的 groovy 文件中调用不同的 Jenkins 作业时传递 Active choice 参数的值,并且该值是 Array/List 类型。

我试过了

build job: "myjob", parameters:[choice(name: 'paramName', choices:['a','b','c'])]

报错

No known implementation of class hudson.model.ParameerValue is using symbol 'choice'

Jenkins pass value of Active Choices Parameter,在这里我找到了一些可以传递主动选择参数值的东西,但在这里它们只传递字符串值,这不是我的情况。

【问题讨论】:

  • 澄清一下:choice参数一次只能有一个值,是string类型的。为什么你有一个清单?您的目标是将选择参数中的可能值作为参数传递给第二个作业吗?
  • @Crash,它是“主动选择参数”,我们可以有多个选择。

标签: jenkins groovy jenkins-groovy


【解决方案1】:

我做了一些研究,但无法将这些类型的参数传递给使用相同参数的第二份工作。我怀疑这是不可能的,因为没有“值”类型可以传递给这个插件参数类。

所以我在这里看到的唯一选择就是在第二个作业中使用字符串参数。我想在第二份工作中你已经对该参数值进行了一些解析,所以你不需要做太多改变:

properties([
    parameters([
        [$class: 'ChoiceParameter',
            choiceType: 'PT_MULTI_SELECT',
            filterLength: 1,
            filterable: false,
            name: 'TEST_PARAM',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [
                    classpath: [],
                    sandbox: false,
                    script: 'return ["Check Jenkins ScriptApproval page"]'
                ],
                script: [
                    classpath: [],
                    sandbox: false,
                    script: 'return ["One","Two:selected"]'
                ]
            ]
        ]
    ])
])

pipeline {
    agent any
    
    stages {
        stage('Test') {
            steps {
                print params.TEST_PARAM
                
                build job: 'test2', parameters: [
                    string(name: 'TEST_PARAM', value: "${params.TEST_PARAM}")
                ]
            }
        }
    }
}

然后在第二份工作中(姓名 = test2):



pipeline {
    agent any

    parameters {
        string(name: 'TEST_PARAM', defaultValue: '')
    }
    
    stages {
        stage('Test') {
            steps {
                script {
                    // split values, guess you're doing this already
                    // since the value of the parameter is also comma-separted
                    // when you get from "Active choice parameter"
                    def testParamValues = "${params.TEST_PARAM}".split(',')
                    
                    testParamValues.each { testParamValue ->
                        print testParamValue.trim()
                    }
                }
            }
        }
    }
}

如果您想在管道中同时使用“主动选择参数”和该字符串参数,您可以检查哪一个的长度 > 0,然后使用该值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多