Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 530
Ana Franco Avatar asked Jan 03 '19 18:01

Ana Franco


People also ask

How does Jenkinsfile use extended choice parameters?

If your Jenkins server is 10.10. 10.10 on port 12345 the URL is 10.10. 10.10:12345/pipeline-syntax Then, On the Sample step dropdown select 'Properties: Set job properties'. There is a checkbox for 'This project is parameterized', then you can select Add parameter > Extended Choice Parameter.

How do you pass a choice parameter in Jenkinsfile?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

Can I mix declarative and scripted pipeline?

It is very much possible, as mentioned in the documentation. In fact it is very much required in my case.


1 Answers

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}"
            }
        }
    }
}
like image 58
hakamairi Avatar answered Oct 23 '22 21:10

hakamairi