Skip to content

Instantly share code, notes, and snippets.

@rhuss
Last active August 12, 2020 19:09
Show Gist options
  • Save rhuss/cefa5f944931d0b313e2 to your computer and use it in GitHub Desktop.
Save rhuss/cefa5f944931d0b313e2 to your computer and use it in GitHub Desktop.
How to execute some shell scripting on Groovy with environment variables and redirection
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()
}
[
"mkdir /tmp/,x",
"echo FROM busybox > /tmp/,x/Dockerfile",
"DOCKER_HOST=tcp://localhost:2375 docker build -t test/test:1.0 /tmp/,x",
"DOCKER_HOST=tcp://localhost:2375 docker push --force=true test/test:1.0",
"rm /tmp/,x/Dockerfile",
"rmdir /tmp/,x",
"DOCKER_HOST=tcp://localhost:2375 docker rmi test/test:1.0"
].each {
exec(it)
}
@vickydhas
Copy link

vickydhas commented Aug 12, 2020

`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()
}

exec("cd ~/Documents/ocbc/knowledgebase.local/")

exec("pwd")`

Error:
pwd
/private/tmp
Expected : pwd value should be this directory passed in parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment