int128 / gradle-ssh-plugin

Gradle SSH Plugin
https://gradle-ssh-plugin.github.io
Apache License 2.0
318 stars 60 forks source link

Why doesn't the remote follow the for loop? #366

Open iamminji opened 3 years ago

iamminji commented 3 years ago

Environment info

2.10.1

first of all, english is not my first language, Please tell me if you don't understand my question

I made a list(In the code below, this is allSessions) as below to manage the host as a file. I put the session in the for loop and proceeded, but it did not work as expected.

The data(Remote) information in the session is different from the data(Remote) information outside the session.

Remote in session does not traverse all session lists. It is fixed as one of the list(allSessions). (I would like to run the deployment in parallel)

ssh.run {  
    for (Remote source : allSessions) {
        println "$source.host"
        session(source) {
            // Why `source` does not change? (`session` function worked, only inside session is different from outside)
            println "$source.host"
        }
    }
}
iamminji commented 3 years ago

I solved it as below.

ssh.run {  
    for (Remote source : allSessions) {
        Remote target = source
        println "$source.host"
        session(target) {
            // `source` and `target` became the same.
            println "$target.host"
        }
    }
}

But I am still wondering why.