Dynamic Parameter

We are looking to run a Jenkins Job using some dropdown-list using parameters that are usually hard-coded.

We are running a Job with “Build using parameters”,currently one of the dropdown parameters is hard-coded using a “Choice Parameter” with 4 existing options/descriptions representing 4 files. We would like to make this a dynamic parameter based off of a query elsewhere.

The query can be done in another Job or if necessary using attributes on the filesystem.

For example, we have a directory of files we want to process and its available on the Jenkins server, we would like to dynamically populate the dropdown with the list of files available to the target directory on Jenkins.

When the list of files increase to 5, the next time we run the Job, we want to see all 5 files.

Are there any modules or techniques that will allow to build a more dynamic list than “Choice Parameter”?

Hi, @rutuparn! We are using Active Choices Parameter plugin similarly. But as files we are using build artifacts of another job. And jobs which should be in choice list contain ImageProducing word in description.

Here is an example from our Jenkinsfile:

// Parameters for Jenkins GUI
properties([
    parameters([
        [$class: 'CascadeChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            name: 'ARTIFACT_PROJECT_NAME',
            description: "Project for copy images from",
            filterable: true,
            filterLength: 1,
            randomName: 'choice-parameter-9587366504630684',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [script: 'return ["Failed to load projects"]'],
                script: [script: '''
def instances = []
for (instance in jenkins.model.Jenkins.instance.getAllItems()) {
    if (instance.getDescription() && instance.getDescription().contains("ImageProducing")) {
        instances.add(instance.displayName)
    }
}
return instances
                '''
                ]
            ]
        ],
        [$class: 'CascadeChoiceParameter',
            choiceType: 'PT_CHECKBOX',
            description: 'Show all builds regardless its state',
            filterLength: 1,
            filterable: false,
            name: 'SHOW_FAILED_BUILDS',
            randomName: 'choice-parameter-5331064039195643',
            referencedParameters: '',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''],
                script: [script: '''
return ['Show all builds']
                '''
                ]
            ]
        ],
        [$class: 'CascadeChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            name: 'ARTIFACT_BUILD_NUMBER',
            description: "Build number for copy images from",
            referencedParameters: 'ARTIFACT_PROJECT_NAME,SHOW_FAILED_BUILDS',
            filterable: true,
            filterLength: 1,
            randomName: 'choice-parameter-9513006304630684',
            script: [
                $class: 'GroovyScript',
                fallbackScript: [script: 'return ["Failed to load build numbers from PROJECT_NAME"]'],
                script: [script: '''
for (instance in jenkins.model.Jenkins.instance.getAllItems()) {
    if (instance.displayName == ARTIFACT_PROJECT_NAME) {
        def builds = []
        for (def build in instance.builds) {
            if (SHOW_FAILED_BUILDS == 'Show all builds') {
                builds.add(build.number)
            } else if (build.getResult().toString().equals("SUCCESS")) {
                builds.add(build.number)
            }
        }
        return builds
    }
}
                '''
                ]
            ]
        ],
        string(name:'IMAGE_FILES', defaultValue:'internal/images/image-*.tar', description: 'This artifact will be copied from build above', trim: true)

And use the following step into pipeline to copy artifacts into workspace:

copyArtifacts filter: params.IMAGE_FILES, projectName: params.ARTIFACT_PROJECT_NAME, selector: (params.ARTIFACT_PROJECT_NAME== 'latest' ? lastSuccessful() : specific("${params.ARTIFACT_PROJECT_NAME}"))

I know, that it is not actually what you looking for, but example may be helpful.

1 Like

@VladyslavKovalov Thank you for reply.