2

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?

Christophe Muller
  • 4,850
  • 1
  • 23
  • 31
dag
  • 1,133
  • 12
  • 25
  • You don't happen to still have the code to initialize the "project" object you're working with here, do you? I need Project properties in a groovy script to provide values for a parameter, it's google hell. – Beaker Apr 07 '23 at 21:14

1 Answers1

3

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().

Christophe Muller
  • 4,850
  • 1
  • 23
  • 31
  • Hi Christophe, thank you for your example, although it currently works only for the master (as the workspace folder does not exist on slaves), i'll try using the env.WORKSPACE Variable of Jenkins. Unfortunately this seems to be tricky too (see http://stackoverflow.com/questions/21236268/access-to-build-environment-variables-from-a-groovy-script-in-a-jenkins-build-st). For now i gave up and have to fill in the default by hand. – dag Apr 23 '15 at 09:12
  • @dag I don't get it.. I only mentioned workspace because I thought that it was what you were trying to reach. BTW, if your job runs on the master the workspace is on the master, but if the job runs in a remote slave then the workspace is on the slave.. Actually in our case, I was able to reach a directory listing in a place that has nothing to do with the workspace and which is located on the remote slave. The groovy code we use is: `"ls -1 /opt/bin-releases".execute().text`. – Christophe Muller Apr 23 '15 at 13:54
  • Hi Christophe, you are right i wanted to access a file in the workspace of the server/slave it was executed the last time running. I don't think it is possible with your proposal as the parameter plugin doesn't know where the last job was run and the proposal you made indicate that a command line version (as `"ls -1 workspace/mytest".execute().text` indicates) works only for the master directory structure, because the workspace dir does not exist on slaves. As i dont have the possibility to test this further this is just a guess. – dag Apr 27 '15 at 14:33
  • Dynamic Parameter Plug-in --- Distribution of This Plugin Has Been Suspended ... This apears in the plugin now ... – Yonsy Solis Oct 18 '17 at 19:04