0

Extended Choice Parameter plugin query: How to load params dynamically from a JSON file for jenkins build job. Here is my jenkins script to add normal parameters and Extended Choice parameters as well.

p

roperties([
        [$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '500']],
        parameters([extendedChoice( 
        defaultValue: 'One,Two,Three,Four', 
        description: '', 
        multiSelectDelimiter: ',', 
        name: 'SAMPLE_EXTENDED_CHOICE', 
        quoteValue: false, 
        saveJSONParameterToFile: false, 
        type: 'PT_CHECKBOX', 
        value:'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten', 
        visibleItemCount: 10)
        ])
])

This is working. But I want to be able to load parameters from a JSON file (using Extended Choice Parameter plugin). How can I modify the above script to accomplish that? PS: Here is the reference for Extended Parameter plugin. https://plugins.jenkins.io/extended-choice-parameter/

1 Answer 1

1

EDIT: Updating my answer to include steps on using the Declarative Directive Generator.

You can experiment with the different available configuration options, and sometimes gets hints as to what certain settings provide. For this specific case:

  1. Navigate to <$JENKINS_URL>/directive-generator/
  2. Select parameters: Parameters from the Sample Directive drop-down.
  3. Click the Add button and select extendedChoice: Extended Choice Parameter
  4. Select JSON Parameter Type and then experiment with the available options.
  5. When finished, click Generate Declarative Directive to generate the code block to paste into your pipeline.

Using the Declarative Directive Generator

You can add the javascriptFile parameter and change the type to PT_JSON.

Using your example:

properties([
    [$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '500']],
    parameters([extendedChoice( 
    defaultValue: 'One,Two,Three,Four', 
    description: '', 
    javascriptFile: '/path/to/file.json',
    multiSelectDelimiter: ',', 
    name: 'SAMPLE_EXTENDED_CHOICE', 
    quoteValue: false, 
    saveJSONParameterToFile: false, 
    type: 'PT_JSON', 
    value:'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten', 
    visibleItemCount: 10)
12
  • I am having Jenkins file (with properties/parameters) in source code of the repo for which I am doing the build. Can I put json file also in the repo.
    – Vineel
    Commented May 19, 2020 at 0:16
  • Do you mean can you have a JSON file within the same repo that's being built? Sure, just checkout the repo then you can read the local file using readJSON() jenkins.io/doc/pipeline/steps/pipeline-utility-steps/…
    – Argyle
    Commented May 20, 2020 at 1:48
  • for JavaScriptFile attribute, we need to give path to the JSON file right? Where should that file exist?
    – Vineel
    Commented May 20, 2020 at 17:20
  • @Vineel please see updated answer for steps on how you can try the different available configuration options to see what works for your situation.
    – Argyle
    Commented May 21, 2020 at 21:13
  • 1) Jenkins file: github.com/vineelyalamarthy/PriceAnalyzer/blob/master/… and 2) my JSON file. 2)github.com/vineelyalamarthy/PriceAnalyzer/blob/master/… This combination didn't seem to work.
    – Vineel
    Commented May 21, 2020 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.