Running script before all in declarative Jenkinsfile to use Extended Choice Parameter plugin

4.6k Views Asked by At

I'm trying to run a script that instantiates the extended choice parameter variable to use it in the declarative jenkinsfile properties section, but I haven't been able to run a script in the jenkinsfile without a step. I don't want to do it as an input step or as a scripted pipeline.

So I'm running it doing first a node step and then a pipeline step, like this:

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 {
        ....
    }
}

And magically it works! with a caveat, only if I have run a build before with only a pipeline block.

So, is there a better way to run a previous script to the pipeline? to be able to create the object for the properties or another place outside the steps to embed a script block?

1

There are 1 best solutions below

2
On

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