Running shell command from Gradle script

If you need to save the command output you could do this:

def gitBranchA = "git branch -a".execute().text.trim()

The command to execute and its arguments must be separate parameters to pass to commandLine, like this:

commandLine 'git', 'branch', '-a'

If you want to execute a complicated pipeline as in your first example, you can wrap it in a shell script.

I cannot test this, but I think this should work as well:

commandLine 'sh', '-c', 'git branch --merged | grep -v -e \* -e master -e develop -e dmz | xargs git branch -D'

Note: I took the liberty and simplified the grep a bit.

Lastly, you could also create a Git alias in your .gitconfig to wrap the complex pipeline.

Tags:

Shell

Gradle