【问题标题】:Access global map variable inside active choice reactive reference parameter访问主动选择反应参考参数内的全局映射变量
【发布时间】:2021-05-23 22:02:41
【问题描述】:

我正在尝试在 jenkins 声明性管道中设置全局映射变量。我正在尝试在 Active Choice Reactive Reference 参数中访问它。我尝试了很多方法来实现这一点,但都没有奏效。

下面是我的示例管道。

def sampleMap= [
    'students' : ['12312312'],
    'teachers' : ['145436436']
]


pipeline {
agent any
stages {
    stage('set params') {
        steps{
           script {
                properties([
                    parameters([
                         [
                            $class: 'CascadeChoiceParameter', 
                            choiceType: 'PT_SINGLE_SELECT', 
                            description: '', 
                            filterLength: 1, 
                            filterable: false, 
                            name: 'Type', 
                            randomName: 'choice-parameter-1325654724334254', 
                            referencedParameters: '', 
                            script: [
                                $class: 'GroovyScript', 
                                fallbackScript: [
                                    classpath: [], 
                                    sandbox: true, 
                                    script: ''
                                ], script: [
                                    classpath: [], 
                                    sandbox: true, 
                                    script: '''
                                        def choices = []
                                        choices.add('students')
                                        choices.add('teachers')
                                        return choices'''
                                ]
                            ]
                        ],
                        [
                            $class: 'DynamicReferenceParameter',
                            choiceType: 'ET_FORMATTED_HTML',
                            description: '',
                            name: 'value',
                            randomName: 'choice-parameter-14347325234254',
                            referencedParameters: 'Type',
                            script: [
                                $class: 'GroovyScript',
                                fallbackScript: [
                                        classpath: [],
                                        sandbox: true,
                                        script: ""
                                        ],
                                script: [
                                    classpath: [],
                                    sandbox: true,
                                    script: '''
                                        def result= ${sampleMap.get(Type)}
                                        return """<input name=\"value\" value=\"${result}\" class=\"setting-input\" type=\"text\">""" 
                                        '''
                                ]
                            ],
                            omitValueField: true
                        ]
                    ])
                ])
           }
        }
    }
}

根据上面的脚本,我有一个全局 Map 变量,其中包含学生列表和教师列表。我有两个名为 Type 和 value 的构建参数。类型是一个下拉菜单,包含 'students' 和 'teachers' 值。根据下拉选择,我想引用全局映射变量并在另一个构建参数中访问它的相应值。

似乎主动选择参数无法访问全局变量。还是语法问题?

谁能帮忙?

谢谢!

【问题讨论】:

  • 试试parameters: ['sampleMap': sampleMap]

标签: jenkins groovy jenkins-pipeline jenkins-groovy


【解决方案1】:

您应该将整个地图传递给脚本并调用它:

[$class: 'CascadeChoiceParameter',
    choiceType: 'PT_SINGLE_SELECT',
    filterLength: 1,
    filterable: false,
    name: 'value',
    referencedParameters: 'Type',
    script: [$class: 'GroovyScript',
        fallbackScript: [
            classpath: [],
            sandbox: true,
            script: 'return ["ERROR"]'
        ],
        script: [
            classpath: [],
            sandbox: true,
            script: """
                def sampleMap = ${sampleMap.inspect()}
                return sampleMap.get(Type)
                """.stripIndent()
        ]
    ]
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多