如何访问Jenkins模板中的参数?

问题描述 投票:0回答:1

我有这个模板:

def call(body) {

    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {

        agent any

        ....

        stages {

            stage('My stages') {
                steps {

                    script {

                        pipelineParams.stagesParams.each { k, v ->
                            if (k.toLowerCase().startsWith("If ") && (params[ k.split(' ')[1] ] as boolean)) {                                
                                stage("$k") {
                                   $v
                                }
                            }
                        }
                    }
                }
            }
        }

        post { ... }
    }
}

以及使用此模板的相对管道:

@Library('pipeline-library') _

pipelineMyTemplate {

    parameters {
        booleanParam(name: 'foo', defaultValue: true, description: 'some option')
        booleanParam(name: 'bar', defaultValue: true, description: 'some option')
    }

    stagesParams = [
        'If foo run': echo "foo is selected",
        'If bar run': echo "bar is selected"
    ]
}

在我编写if (some option selected in the parameters)的管道中,如果还没有的话我该如何引用这些参数?另外我还注意到,以这种方式创建时,这些选项未在UI中显示为复选框。

jenkins jenkins-pipeline jenkins-groovy jenkins-job-dsl
1个回答
0
投票
    我认为管道脚本没有布尔参数。因此,作为一种解决方法,您需要尝试`parameterAsBoolean =(BAR ==“ true”)
  1. 据我所知,您不可能一步一步走,所以请查看以下内容是否对您有用?
  • def call(body) {

    def pipelineParams= [:] body.resolveStrategy = Closure.DELEGATE_FIRST body.delegate = pipelineParams body() pipeline { agent any .... stages { script { pipelineParams.stagesParams.each { k, v -> if (k.toLowerCase().startsWith("If ") && (params[ k.split(' ')[1] ] as boolean)) { stage("$k") { sh "$v" } } } } } post { ... } }}

  • © www.soinside.com 2019 - 2024. All rights reserved.