8

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?

Ana Franco
  • 1,611
  • 3
  • 24
  • 43

1 Answers1

1

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}"
            }
        }
    }
}
hakamairi
  • 4,464
  • 4
  • 30
  • 53
  • 1
    Have you seen the ExtendedChoiceParameter plugin? it doesn't have an option for the parameters block in the declarative Jenkinsfile and there are some cool options that come with that plugi, like multiselect and uploading options from a file – Ana Franco Jan 14 '19 at 13:49
  • I was afraid you would say so. I haven't found a pretty way, if the number of parameters is fixed (and the multiselect might be joined then split) maybe you should consider dividing this into two jobs. Parameter collection and trigger of the job with appropriate parameters chosen. – hakamairi Jan 14 '19 at 14:17