1

I am able to execute a shell script from build section in Jenkins, by performing cd to the directory and executing the script residing on my Linux Machine.

However when I am trying to populate Active Choices parameter, by executing the shell script from a groovy script it is not working no matter how many ways I try.

The output is always java.lang.UNIXProcess

Please help. I have been trying from past few days with no success

* Below are few from what I have tried:*

def command =['/bin/sh',  '-c',  'ssh user@host '/path/to/script.sh ' ']

def command = 'bash /path/to/script.sh'

I am expecting it to return output of the script.

When I execute only "ls" command, as in def command = 'ls' then it is listing Jenkins files under home directory but not scripts'


to add more on the above. When I am trying to switch to the current workspace it says directory not found.

And I am trying to write script for Active Choice Parameters, I am not sure how to make scope of script to point to Work space.

My assumption is If I am able to switch to the Workspace directory, I may able to execute the shell script residing on my Linux machine

2 Answers2

0
def proc = "ssh user@host '/path/to/script.sh'".execute()
proc.waitFor()
println "Exit code: ${proc.exitValue()}"

try with this!?

r4vanan
  • 51
  • 5
0

From what I understand is that you try to populate an Active Choices parameter with a Groovy script in the parameter settings by executing a command on your Jenkins master.

Implied you are doing it in the GUI, a very basic example as starting point for a single select could be:

def proc = 'echo Test'.execute()

return [proc.text]

A simple example with a shell script could be:

def proc = '/var/jenkins_home/test.sh'.execute()

return [proc.text]

Where Script content is:

echo "TestFromScript"

Note that you have to return the output of your process to the plugin and that this example does not cover any error handling.

Also if you try to run a shell script, you need the full path to the script and the script must have the correct settings to be executable.

You can find more on executing with Groovy here:

Groovy executing shell commands

Franco
  • 193
  • 1
  • 9
  • the above works. However, when I try to execute shell script, the same issue persists – Deepthi Kasturi Mar 31 '23 at 13:17
  • I updated my answer with a working example of a shell script. You may have to adopt it to your needs. If still unclear, please provide more information of your setup, like where you want to run the script from, script contents, errors... – Franco Mar 31 '23 at 13:55
  • sure. Jenkins is in remote machine. My linux machine is added as node. So there exists two nodes. One is master and one is mine. Usually when I trigger the build it builds remotely on my node. In this case, the job is parameterized. I installed Active Choice plugin -> and for this parameter, I have written a groovy script which should, execute shell script (residing on my linux machine) and populate the choices. User will be selecting one of the choices before triggering the build. Issue is I am unable to execute the script and observed that "PWD" shows Jenkins Home path – Deepthi Kasturi Mar 31 '23 at 14:50
  • From google search, I found that Groovy will be pre-installed and not sure if there is any other configurations required. – Deepthi Kasturi Mar 31 '23 at 14:52
  • That's expected behavior if you try to run a script just like that. The Groovy script you use to populate is always executed on the master, so it also tries to find your sh script there, while your build "can" run somewhere else (e.g. your node). Ideally you should find another way to populate you choices, maybe plain Groovy without bash. Your approach sounds very error prone. But if you have to go that way, you'll have to work out something with remote executing the bash script. Then always keep in mind that your Groovy is executed on the master. – Franco Apr 01 '23 at 17:02