How to run shell scripts in a cluster-operation

Issue

I want to run bash script as a cluster-operation, but I don’t see the shell step available.

Environment

Resolution

Add the step Execute Groovy Script on Master in the cluster-operation, then use groovy as a wrapper to execute the bash script, for example:

def exec(cmd) {
  println cmd
  def process = new ProcessBuilder([ "sh", "-c", cmd])
                                    .directory(new File("/tmp"))
                                    .redirectErrorStream(true) 
                                    .start()
  process.outputStream.close()
  process.inputStream.eachLine {println it}
  process.waitFor();
  return process.exitValue()
}
 
[
  "echo hello world",
  "ls -al"
].each {
  exec(it)
}

Workaround

Create a freestyle / pipeline job on the masters. Use this job to run the bash script using the shell step.
Create a regular job on CJOC and use Cluster-wide job trigger to trigger the newly created jobs on your masters.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.