How to execute shell script from Jenkins groovy script in parameters?

I want to call a shell script in the Uno-Choice dynamic reference parameter and do some operation (create some files and call another shell scripts from the called shell script).

Currently, I can call the shell script and cat some files, but I can not create new files or call another shell script from this.

def sout = new StringBuffer(), serr = new StringBuffer() // 1) def proc ='cat /home/path/to/file'.execute() //display contents of file // 2) def proc="sh /home/path/to/shell/script.sh".execute() //to call a shell script but the above dosent work if I echo some contents //into some file. proc.consumeProcessOutput(sout, serr) proc.waitForOrKill(1000) return sout.tokenize() 

for example: - in script.sh if you add a line

 echo "hello world" > test 

then the test file is not created

for better understanding:

Groovy shell command execution

http://jenkins-ci.361315.n4.nabble.com/Executing-a-shell-python-command-in-Jenkins-Dynamic-Choice-Parameter-Plugin-td4711174.html

+6
source share
1 answer

Since you are using bash scripts from the groovy wrapper, stdout and stderr are already redirected to the groovy shell. To override this, you need to use exec inside the shell script.

eg:

groovy script:

 def sout = new StringBuffer(), serr = new StringBuffer() def proc ='./script.sh'.execute() proc.consumeProcessOutput(sout, serr) proc.waitForOrKill(1000) println sout 

A shell script called script.sh and is located in the same folder:

 #!/bin/bash echo "Test redirect" 

Running groovy with the shell script above will cause Test redirect to exit to the standard output of the groovy script

Now add the stdout redirect from exec to script.sh`:

 #!/bin/bash exec 1>/tmp/test echo "Test redirect" 

Now running the groovy script will create the /tmp/test file with the content of Test redirect

You can learn more about I / O redirection in bash here.

+13
source

Source: https://habr.com/ru/post/987255/


All Articles