Jenkinsfile Groovy管道文本参数空白

时间:2018-04-24 09:57:49

标签: jenkins jenkins-pipeline jenkins-groovy

目前我希望将多线文本参数添加到常规管道中。如果text参数没有左列(参数之前没有空格),则将空格注入文本参数列表。

有关如何解决此问题的任何想法?

这是代码

#!/usr/bin/env groovy

node {

    def startTime = new Date()
    println "Build start time : " + startTime

    // Load system parameters
    def projectProperties = [
    [$class: 'EnvInjectJobProperty', info: [loadFilesFromMaster: false, secureGroovyScript: [classpath: [], sandbox: false, script: '']], keepBuildVariables: true, keepJenkinsSystemVariables: true, on: true]
    ]

    // Set project parameters  
    projectProperties.add(parameters([
        string(name: 'infraRepo', description: 'Repo Name', defaultValue: 'my-infrastructure' ),
        string(name: 'infraBranch', description: 'Repo Branch', defaultValue: 'develop' ),
        string(name: 'projectName', description: 'Project name', defaultValue: 'think-more' ),
        // Text field not left side aligned now whitespace will be injected
        text(name: 'ecrRepoAndVersion', description: 'ECR Docker name and version number',
        defaultValue:'''address=3.0.1
        address-details=3.0.1
        auth=3.2.1'''),
        choice(name: 'clusterName', description: 'Ecs cluster name', choices: '---Select---\nblue-ci\ngreen-ci', defaultValue: '---Select---'),
]))

    properties(projectProperties)

    // Print system variables
    sh 'env | sort'
}

这是Jenkins Job UI在执行此管道后如何看待的图像。请注意ecrRepoAndVersion字段中的空格。

Jenkins UI View

2 个答案:

答案 0 :(得分:0)

谢谢 - 这完美无缺。

text(name: 'ecrRepoAndVersion', description: 'ECR Docker name and 
version number',defaultValue:"""address=3.0.7-RC\n
address-details=3.0.3-RC\nauth=3.2.3-RC""")

答案 1 :(得分:0)

撇开这种逻辑的需要,我将加入项目列表而不是逐字逐句说明,从而增加了可读性和易于维护性:

def ecrRepoAndVersionItemsDefault = [
    "address=3.0.7-RC",
    "address-details=3.0.3-RC",
    "auth=3.2.3-RC",
]
...
// then construct an ArrayList
def jobParams = []
jobParams << ...
...
jobParams << text(
    name: 'ecrRepoAndVersion',
    description: 'ECR Docker name and version number',      
    defaultValue: ecrRepoAndVersionItemsDefault.join('\n')
)
// then add the properties
...
projectProperties.add(parameters(jobParams))
...
properties(projectProperties)
... 
// etc.