首页 > 解决方案 > 在声明性 Jenkinsfile 中首先运行脚本以使用扩展选择参数插件

问题描述

我正在尝试运行一个脚本来实例化扩展选择参数变量以在声明性 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-pipelinejenkins-declarative-pipelineextended-choice-parameter

解决方案


I would rather go for parameters block in pipeline.

The parameters directive provides a list of parameters which a user should provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the params object, see the Example for its specific usage.

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}"
            }
        }
    }
}

推荐阅读