I would like to run a python script to gather list of data and then use is as my dropdown values. Is it possible to run such a script with opening jenkins job? So the user will see dynamically set dropdown outputs? Now I have all dropdowns added manually, but would have to change configuration every time when one of the options is changed or added. Is there available plugin to gather this data every time?
Asked
Active
Viewed 317 times
-1
-
Have a look at [Extensible Choice](https://plugins.jenkins.io/extensible-choice-parameter/) plugin. You can provide choices from a variety of sources, e. g. a file or system Groovy script. It you have constant choice values, a file would be sufficient. – zett42 Dec 21 '22 at 11:56
1 Answers
0
Update 22.12.2023
I was wrong. If python is installed on the node, where the parameter is executed, you can do something like:
choiceParameter {
name('pythonParams')
description('Select parameter from the dropdown list')
choiceType('PT_SINGLE_SELECT')
filterable(false)
filterLength(0)
randomName(null)
script {
groovyScript {
script {
script("""
def parametersFromScript = ['/usr/bin/python3', "path/to/your-script.py"].execute()
return parametersFromScript
""")
sandbox(true)
}
fallbackScript {
script("return ['ERROR: Could not get parameters']")
sandbox(true)
}
}
}
}
Note: The user, that is running this script, needs access to the python binary and script.
I never seen something like this. I think, you cannot call python directly form inside parameter script as it is running in a sandbox. So you can only execute groovy code there. But what you can do in the parameter scripts is making HTTP requests.
I would suggest deploying your python code as a little service and calling it via API. Like described here.
If you want your groovy code do be versioned as well I would suggest parameter script in declarative pipeline like https://stackoverflow.com/a/45477285/5138605.

Haagy
- 61
- 1
- 9