I have a Ktor server, and I want to deploy it on a VPS. I generated public and private keys in my system, also added the public key to the VPS SSH,
Now in my server code I wrote some script in my build.gradle file to deploy the server directly to my VPS.
In build.gradle
val sshAntTask = configurations.create("sshAntTask")
ant.withGroovyBuilder {
"taskdef"(
"name" to "scp",
"classname" to "org.apache.tools.ant.taskdefs.optional.ssh.Scp",
"classpath" to configurations.get("sshAntTask").asPath
)
"taskdef"(
"name" to "ssh",
"classname" to "org.apache.tools.ant.taskdefs.optional.ssh.SSHExec",
"classpath" to configurations.get("sshAntTask").asPath
)
}
task("deploy") {
dependsOn("clean", "shadowJar")
ant.withGroovyBuilder {
doLast {
val knownHosts = File.createTempFile("knownhosts", "txt")
val user = "root"
val host = "server_ip" //here I have my VPS server's IP
val key = file("keys/prvtkey") //this file is present in the mentioned directory
val jarFileName = "myfile.jar"
try {
"scp"(
"file" to file("build/libs/$jarFileName"),
"todir" to "$user@$host:/root/serverFiles",
"keyfile" to key,
"trust" to true,
"knownhosts" to knownHosts
)
"ssh"(
"host" to host,
"username" to user,
"keyfile" to key,
"trust" to true,
"knownhosts" to knownHosts,
"command" to "mv /root/serverFiles/$jarFileName /root/serverFiles/myfile.jar"
)
...
I have this above code which doesn't have any errors. When I run the command in Android Studio's terminal ./gradlew this works fine with no error as it just runs the Gradle tasks
But when I run the command ./gradlew deploy I got error as Auth Cancel
I have attached a screenshot of what I'm getting in terminal.
For clarity I have this prvtkey (the SSH private key) in my project, which is mentioned in code. Also I have added the public key to my VPS SSH's
If you think the connection to this IP might not be working then I tried this command.
ssh root@*my_server_ip* OR ssh -i prvtkey root@*my_server_ip* also works
//asks for password
//then connection establishes
I think I haven't have any password or something to Authenticate to Server, maybe thats the reason for this error but I have private and public key to do that part but it doesn't works. If I'm correct then suggest a solution, or correct me if I'm wrong, as I am new to Ktor and also in VPS server hosting Terminal screenshot
[EDIT]
When i test the connection using terminal opened in .ssh directory
ssh -t prvtkey root@server_ip //this works without password
but when i tested the same command from the project directory's keys/prvtkey i got error for file permissions and it asks for password
Now I have corrected that and also tested the connection from the project
ssh -i keys/tictackey root@server_ip //now it goes well without password
The error remains the same This Might help you to understand the keys configs