【问题标题】:Running script before all in declarative Jenkinsfile to use Extended Choice Parameter plugin在声明性 Jenkinsfile 中首先运行脚本以使用扩展选择参数插件
【发布时间】:2023-03-16 01:25:01
【问题描述】:

我正在尝试运行一个脚本来实例化扩展的选择参数变量以在声明性 jenkinsfile 属性部分中使用它,但是我无法在 jenkinsfile 中运行脚本而没有一步。我不想将其作为输入步骤或脚本管道。

所以我运行它首先是一个节点步骤,然后是一个管道步骤,如下所示:

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition

node('MyServer') {

    try {
        def multiSelect = new ExtendedChoiceParameterDefinition(...)   

        properties([ parameters([ multiSelect ]) ])
    }
    catch(error){
        echo "$error"
    }
}

pipeline {

    stages {
        ....
    }
}

神奇地它起作用了!需要注意的是,只有当我之前只使用管道块运行过构建时。

那么,有没有更好的方法将之前的脚本运行到管道中?能够在嵌入脚本块的步骤之外为属性或其他地方创建对象?

【问题讨论】:

    标签: jenkins-pipeline jenkins-declarative-pipeline extended-choice-parameter


    【解决方案1】:

    我宁愿选择parameters block in pipeline

    parameters 指令提供了一个用户参数列表 触发管道时应提供。这些值 用户指定的参数可通过流水线步骤使用 params 对象,具体用法见示例。

    pipeline {
        agent any
        parameters {
            string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
    
            text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
    
            booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
    
            choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
    
            password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
    
            file(name: "FILE", description: "Choose a file to upload")
        }
        stages {
            stage('Example') {
                steps {
                    echo "Hello ${params.PERSON}"
    
                    echo "Biography: ${params.BIOGRAPHY}"
                    echo "Toggle: ${params.TOGGLE}"
    
                    echo "Choice: ${params.CHOICE}"
    
                    echo "Password: ${params.PASSWORD}"
                }
            }
        }
    }
    

    【讨论】:

    • 你见过 ExtendedChoiceParameter 插件吗?它没有声明式 Jenkinsfile 中的参数块选项,并且该插件附带了一些很酷的选项,例如多选和从文件上传选项
    • 我担心你会这么说。我还没有找到一个很好的方法,如果参数的数量是固定的(并且多选可能会被加入然后拆分)也许你应该考虑将它分成两个工作。选择适当参数的作业的参数收集和触发。
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多