Jenkins dynamic build parameters using project workspace resources

7.4k Views Asked by At

i am trying to establish some dynamic build parameters for a couple of Jenkins Jobs. I already found Dynamic Parameter Plug-in and Extensible Choice Parameter plugin. Both are able to use groovy scripts for their input.

Now comes the tricky part. I want to read a project.property file (the current version) and use it as default for the input. The problem was to get access to the Files in the workspace. Only the "Extensible Choice Parameter" plugin had a option to enable a convinience variable for the current project (AbstractProject) where i can get the FilePath for the workspace. See the following script:

import hudson.FilePath;

propertiesFile = new FilePath(project.getWorkspace(),"project.properties");
props = new Properties();
props.load(propertiesFile.read());
return [props["version"]]

Now this plugin only offers a list to the user (even though i can make it editable) and the major drawback is, i need to copy and paste this script to every job i want to use it (not to mention changing something for all these jobs will be a pain too).

Dynamic Parameters Plugin has a nice scriptler integration but i dont see how to get the current project. The examples do not show anything that loads the parameters from the actual workspace but only from external files.

Anyone knows how to solve either of these issues?

1

There are 1 best solutions below

4
On

I found that using the Dynamic Parameter Plug-in instead of Extensible or Extended Parameters allows to run groovy scripts either:

  • on the master, or
  • on the slave on which the job runs (provided that the slave is up).

This choice is made with the Remote Script checkbox: from the plugin doc: "if the "Remote Script" check-box is checked, then the script will be executed on the slave where the build is started."

For example, I have successfully listed files in the workspace and make a parameter menu with them using this script (mytest being my job name):

def dir = "ls -1 workspace/mytest".execute().text
return dir.readLines()

Note: contrary to other parameter plugins expecting csv texts separated by commas (Extended Parameter Plugin), this one expects a groovy list object, hence the call to readLines().