0
votes

I'm using groovy to get some parameters to run jobs in jenkins.

My question is why script A work's and B and C don't

A

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ls -D --format=single-column /path/folder'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"
return sout.tokenize()

B The \\ is to escape \

def proc = 'find /path/folder/ -type f \\( -iname \\*.ear -o -iname \\*.war \\)'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"
return sout.tokenize()

C script.sh return a list of files

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = '/scripts/script.sh'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"
return sout.tokenize()

There is some jenkins restriction to run scripts from groovy or certainly shell commands?

1
In what way do the scripts not work? - doelleri
B and C doens't show anything, In the jenkins log I can see the list generated (println "out> $sout err> $serr") but I got nothing in Jenkins. - Joao Vitorino

1 Answers

0
votes

After a lot of tries I can make this work.

if (Origem.equals('FILE')) {
def cmd = ["/bin/bash","-c","/opt/scripts/SearchPackage.sh"]
def sout = new StringBuffer()
def serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput ( sout, serr )
proc.waitForProcessOutput ()
return sout.tokenize()
}

Groovy was not recognizing the // character as an escape for '/'. I just decide put everything in a shell script and use groovy to call this script. THe problem was solved but if someone has a better aproach I will be glad.