sugar
sugar

Reputation: 251

executing shell script and using its output as input to next gradle task

I am using gradle for build and release, so my gradle script executes a shell script. The shell script outputs an ip address which has to be provided as an input to my next gradle ssh task. I am able to get the output and print on the console but not able to use this output as an input to next task.

remotes {
  web01 {
    def ip = exec {
    commandLine './returnid.sh'
    }
    println ip  --> i am able to see the ip address on console
    role 'webServers'
    host = ip  --> i tried referring as $ip '$ip' , both results into syntax error
    user = 'ubuntu'
    password = 'ubuntu'
  }
}

task checkWebServers1 << {

  ssh.run {
    session(remotes.web01) {
    execute 'mkdir -p /home/ubuntu/abc3'
}
}
}

but it results in error "

What went wrong:
Execution failed for task ':checkWebServers1'.
 java.net.UnknownHostException: {exitValue=0, failure=null}"

Can anyone please help me use the output variable in proper syntax or provide some hints which could help me.

Thanks in advance

Upvotes: 3

Views: 4630

Answers (1)

Stanislav
Stanislav

Reputation: 28126

The reason it's not working is the fact, that exec call return is ExecResult (here is it's JavaDoc description) and it's not a text output of the execution.

If you need to get the text output, then you've to specify the standardOutput property of the exec task. This could be done so:

remotes {
    web01 {
        def ip = new ByteArrayOutputStream()
        exec {
            commandLine './returnid.sh'
            standardOutput = ip
        }
        println ip
        role 'webServers'
        host = ip.toString().split("\n")[2].trim()
        user = 'ubuntu'
        password = 'ubuntu'
    }
}

Just note, the ip value by default would have a multiline output, include the command itself, so it has to be parsed to get the correct output, For my Win machine, this could be done as:

ip.toString().split("\n")[2].trim()

Here it takes only first line of the output.

Upvotes: 1

Related Questions