5

I'm using Jenkins to launch a script on a linux machine.

When I run this manually on the server, it works:

/bin/bash -c '/some/script MyProduct SomeBranch'

When I run this with groovy, it doesn't work.

I get the same error as if I didn't pass the "-c" option, so somehow the "-c" isn't working.

Here is my code:

branchName = "SomeBranch"
configName = "release"
println "Building for branch "+branchName+" and configuration "+configName

def chkbranch = { String product, String branch -> mkcmd( product, branch ) } 
private def mkcmd ( String product, String branch ) {
  // Build the command string to run
      def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
      def sout = new StringBuffer()
      def serr = new StringBuffer()
  // Run the command
      println "running "+cmd
      def proc = cmd.execute()
      proc.consumeProcessOutput ( sout, serr )
      proc.waitForProcessOutput ()
      println "out> $sout"
      println "err> $serr"
      return sout
}

chkbranch ( "MyProduct", branchName )

Is this the correct way to build the command in Groovy?:

def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
cmd.execute()

Thanks!

Question similar to / helpful resources that I tried:

Community
  • 1
  • 1
Katie
  • 45,622
  • 19
  • 93
  • 125

1 Answers1

7

Try to run the command in the following way:

def cmd = ["/bin/bash", "-c", "/some/script", product, branch]

You may also try:

def cmd = ["/some/script", product, branch]

if /some/script is executable - BTW is it placed under root (/)?

Opal
  • 81,889
  • 28
  • 189
  • 210
  • This worked! Now it's saying "permission denied" for accessing a file within the script, but it's running the script this time! Yes the script is in root, is there anything special I need to worry about for that? – Katie Mar 25 '15 at 18:53
  • Rather not. I thought about permissions :) – Opal Mar 25 '15 at 18:54
  • 1
    it must be `["/bin/bash", "-c", "/some/script $product $branch"]...` - the shell command to run is the sole paramenter after `-c`. and take double care, what product and branch are. if product is `rm -rf /` it will run – cfrick Mar 26 '15 at 07:59