Home > Back-end >  How to send a referencedParameter to a readFileFromWorkspace through activeChoiceReactiveParam
How to send a referencedParameter to a readFileFromWorkspace through activeChoiceReactiveParam

Time:03-20

I'm trying to send a referencedParameter ('product') to a Groovy script (services.groovy) that is triggered by the command readFileFromWorkspace that is wrapped with an activeChoiceReactiveParam.

Expected result: Get a dropdown list with the files content.
Actual result: The job fails when processing the DSL script

ERROR: (services.groovy, line 5) No such property: product for class: dsl.jobs.argocd.services

I tried to define the products referenced parameter as an environment variable (And updated in the services.groovy script), it didn't work.
I tried to re-create the services.groovy file in the directory /tmp/ but I had issues finding the files.

products.groovy:

package dsl.jobs.argocd

return ['a','b','c']

services.groovy:

package dsl.jobs.argocd

return_value = []

if (product.equals("a")){
    return_value = ['e']
}
if (product.equals("b")){
    return_value = ['f']
}
if (product.equals("c")){
    return_value = ['g']
}
return return_value;

Pipeline:

pipelineJob("test") {
    description("test")
    keepDependencies(false)
    parameters {
        activeChoiceParam('product') {
            description('What product would you like to update?')
            filterable()
            choiceType('SINGLE_SELECT')
            groovyScript {
                script(readFileFromWorkspace('dsl/jobs/argocd/products.groovy'))
                fallbackScript('return ["ERROR"]')
            }
        }
        activeChoiceReactiveParam('service') {
            description('Which services would you like to update?')
            filterable()
            choiceType('CHECKBOX')
            groovyScript {
                script(readFileFromWorkspace('dsl/jobs/argocd/services.groovy'))
                fallbackScript('return ["ERROR"]')
            }
            referencedParameter("product")
        }
    }
}

Am I approaching this wrong? Is there a different way to use the same parameter in a few Groovy files?

CodePudding user response:

Well, seems the code above is perfect, the only problem was the location of the services.groovy script.

I took the file out of the DSL directory (Since I don't want it to be parsed as a DSL file), referenced it to the correct location, and it works perfect.

Updated pipeline:

pipelineJob("test") {
    description("test")
    keepDependencies(false)
    parameters {
        activeChoiceParam('product') {
            description('What product would you like to update?')
            filterable()
            choiceType('SINGLE_SELECT')
            groovyScript {
                script(readFileFromWorkspace('dsl/jobs/argocd/products.groovy'))
                fallbackScript('return ["ERROR"]')
            }
        }
        activeChoiceReactiveParam('service') {
            description('Which services would you like to update?')
            filterable()
            choiceType('CHECKBOX')
            groovyScript {
                script(readFileFromWorkspace('services.groovy'))
                fallbackScript('return ["ERROR"]')
            }
            referencedParameter("product")
        }
    }
}
  • Related