Bash Shell output as Jenkins Choice Parameter

21 views
Skip to first unread message

Kenny Cason

unread,
Sep 15, 2020, 6:43:00 PM9/15/20
to Jenkins Users
I'm running Jenkins 2.251 on Ubuntu.
I have a shell script (script.sh) that outputs a list: 
$ ./script.sh
[item1,item2,item3]

In my freestyle project I've added a build step that executes script.sh, and I see the output in the build log just like the above example. I don't care if the final project is freestyle, but that worked to get me to this point.

Now, I want to use this list as the input to a Choice parameter so it creates a dropdown chooser with the items in the list. I've marked the project as parameterized and added a Choices parameter but there is no linkage between the list above and the Choices parameter. I would like to know how to use the output from script.sh as the input to the Choices dropdown.

I found a groovy example here that kinda does what I want but I don't need to rewrite the groovy code to produce the list, it's all done in script.sh:

Any help is appreciated!

Thanks,

-Kenny Cason

Gianluca

unread,
Sep 16, 2020, 2:46:54 AM9/16/20
to Jenkins Users
Hi Kenny,
I think you still need to get the output from shell script to groovy and then parse it.
The "sh" command has a parameter to return the stdout of the script.

So, starting from the Groovy script you mentioned ... then I would remove all code related to "cmd.execute" and I will change with a line that call the sh command:

sout = sh( script: "./script.sh", returnStdout: true )

I would make sure the script.sh returns a JSON ... and then continue from line 31 of the example script as it is.

Cheers,
Gianluca.
Reply all
Reply to author
Forward
0 new messages