Our Spring Boot (with Gradle) application has upstreams, that are hosted in the cloud. So, when we need to communicate with them from the local machine, we use SSH tunnels to test app locally.
So, usually, if I need to test something that requires a server from a restricted zone, I do following:
1. Start SSH tunnel using ssh -N myhost
from cmd console
2. Run application from Intellij Idea using Soring Boot Run/Debug configuration.
3. Close tunnel (Ctrl+C in command line window) after I finished.
My questions:
- Can I do these steps directly from IntelliJ Idea, setting up some Run/Debug config that will start the tunnel, run the app, and close it after my application is dead?
- Can I set up the same in Gradle, create some task for tunneling and run it like:
./gradlew startTunnel bootRun
Thanks in advance.
You can try using the Gradle spawn plugin like described here
Define theses tasks (the -v
option is needed to match the Authentication succeeded string) :
import com.wiredforcode.gradle.spawn.*
task startSSHTunnel(type: SpawnProcessTask) {
command "ssh -N -v myhost"
ready 'Authentication succeeded' // this is printed by the SSH session's debug trace when connection has been successful
}
task stopSSHTunnel(type: KillProcessTask)
bootRun.finalizedBy stopSSHTunnel
And then just run it :
./gradlew startSSHTunnel bootRun
Maybe you would have to set up some adjustments but the main spirit is here.