How to run shell scripts in a cluster-operation

Article ID:360020737392
1 minute readKnowledge base

Issue

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

Resolution

Add the step Execute Groovy Script on controller 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 controllers. 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 controllers.