0

I am trying to implement active reactive choice parameter . In reactive parameter basically I am hard coding the different options based on the active parameter

Below is the sample code

if (Target_Environment.equals("Dev01")) {
  return ["test_DEV"]
} else if (Target_Environment.equals("Dev02")) {
  return ["test3_DEV02","test2_DEV02"]
} else if (Target_Environment.equals("Dev03")) {
  return ["test3_DEV03"]
} else if (Target_Environment.equals("Sit03")) {
  return ["test3_SIT03"]
}else if (Target_Environment.equals("PPTE")) {
  return ["test3_PPTE"]
}  

else {
  return ["Please Select Target Environment"]
}

Instead hard coding the choices I want to read from a file in jenkins workspace and show the content as the choices , what would be an ideal way to go with that ? The readFile is not working under return function

I am also trying with extended choice parameter but there I am passing a property file with filename however how can I pass the property file with if else condition

sakhdeek
  • 25
  • 8
  • Possible duplicate of [Reading file from Workspace in Jenkins with Groovy script](https://stackoverflow.com/questions/22917491/reading-file-from-workspace-in-jenkins-with-groovy-script) – Mebin Joe Apr 03 '19 at 05:11
  • if I pass like below if (Target_Environment.equals("Dev01")) { return [ readFile Filename ] } it doesnt work . I know how to read file but not sure how to pass the content in reactive choice parameter so its not a duplicate question – sakhdeek Apr 03 '19 at 05:22
  • Have you found a way to do it ? I'm actually struggling with it right now and i'm affraid that you can't read from your workspace outside of the pipeline{} step – Rolexel Jan 13 '20 at 15:34

1 Answers1

0

I'm not 100% sure if this is accurate. But I would expect that you can't read from the job workspace in a parameter like that because workspaces are created after a build (see below error message).

So if you create the job with no previous builds, there will be no workspace file to read from?

Whenever I have seen that parameter type used in the past, they usually read from a file on the server instead of the workspace.


Error message from Jenkins jobs that have no previous builds:

Error: no workspace

A project won't have any workspace until at least one build is performed.

Run a build to have Jenkins create a workspace.
hhami
  • 176
  • 10
  • I am fine with that , I can read from a file but even how can I do that in the return statement of a reactive choice parameter – sakhdeek Apr 03 '19 at 10:16
  • Again, not 100% confident on the parameter as I haven't really used it but I would assume that something like the below may work for you: ```def list = [] def lines = file.readLines() lines.each { String line -> list << line } return list``` This will return a list of things for the user to select from the file. Another parameter you could use is the Extended Choice Parameter, it allows you to have the key/value be different. i.e. the user will see and select `Dev01` but the value used by Jenkins will be `test_DEV`. – hhami Apr 03 '19 at 10:54