使用声明管道的Jenkins参数分隔符

时间:2018-05-24 07:47:32

标签: jenkins

我的声明性jenkins管道中有50个参数,能够用分隔符创建组会很棒。

我看到了这个插件:https://wiki.jenkins.io/display/JENKINS/Parameter+Separator+Plugin

但它只使用GUI,我需要在代码中声明它,是否可能? 编辑:这是代码: 我只把8个字段缩短了,但是8个或50个,它应该是相同的

#!/bin/groovy

pipeline {
    agent { label 'ansible24' }

    parameters {
        // init script params
        string(name: 'NAME', defaultValue: 'Nightly Valid', description: '')
        string(name: 'CODE', defaultValue: 'NIGHTLY', description: '')
        string(name: 'VERSION', defaultValue: '1.0.0', description: ' ')
        string(name: 'URL', defaultValue: "https://my.gitlab.net/config-daily.git", description: '')
        // What I need is programmatically add a separator like:
        // addSeparator()
        string(name: 'ADMIN_MAIL', defaultValue: 'mail@as.com', description: '')
        string(name: 'PUBLIC_DOMAIN', defaultValue: 'domain.com', description: '')
        string(name: 'PROVIDER', defaultValue: 'rhone', description: ' Define target virtualization provider. values : rhone, no-infra (read carefully use case)')
        choice(name: 'BASTION_STATE', choices: "present\nabsent", description: "Bastion state ")
    }

    stages {
        stage("Run Init script") {
            steps {
                script {
                    def filename = "${WORKSPACE}/${params.CODE}/configuration.yml"
                    def yaml = readYaml file: filename

                    // General data
                    yaml.global.name = "${params.NAME}".toString()
                    yaml.global.code = "${params.CODE}".toString()
                    yaml.global.admin_mail = "${params.ADMIN_MAIL}".toString()
                    yaml.global.public_domain = "${params.PUBLIC_DOMAIN}".toString()
                    yaml.global.provider = "${params.PROVIDER}".toString()
                    yaml.global.bastion.state = "${params.BASTION_STATE}"
                    sh "rm $filename"

                    writeYaml file: filename, data: yaml
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您将在此处找到解决方案:https://stackoverflow.com/a/44822379/2910155

您可以在jenknisfile顶部的声明性管道中(而不是在管道{...}之前)在声明性管道中定义参数,就像在共享链接中得到答复一样。

因此,要在声明性管道中使用参数分隔符插件,您需要执行以下操作:

String sectionHeaderStyle = '''
color: white;
background: green;
font-family: Roboto, sans-serif !important;
padding: 5px;
text-align: center;
'''

String separatorStyle = '''
border: 0;
border-bottom: 1px dashed #ccc;
background: #999;
'''

properties([
    parameters([
       [
        $class: 'ParameterSeparatorDefinition',
        name: 'FOO_HEADER',
        sectionHeader: 'Foo Parameters',
        separatorStyle: separatorStyle,
        sectionHeaderStyle: sectionHeaderStyle
       ]
    ])
])

pipeline {
    ...
}

答案 1 :(得分:0)

我会尝试在Groovy中直接操作参数数组。

一些例子:

for epochs in range(hm_epochs):
  loss = 0 # Issue here

管道你无论如何都在使用groovy。