我是编程新手。
我使用 ktor 构建了一个后端,现在我想将该应用程序部署到服务器。
我曾尝试在虚拟专用服务器上多次部署它,但每次出现错误 com.jcraft.jsch.JSchException: Auth fail.
我正在使用 ssh 密钥进行身份验证。
我的 Build.gradle.kts 文件
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
val kmongo_version:String by project
val commons_codec_version:String by project
plugins {
kotlin("jvm") version "1.7.21"
id("io.ktor.plugin") version "2.1.3"
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.21"
id("com.github.johnrengelman.shadow") version "5.2.0"
}
group = "com.androidjunior9"
version = "0.0.1"
application {
mainClass.set("io.ktor.server.netty.EngineMain")
project.setProperty("mainClassName", mainClass.get())
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
maven { url=
uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap")
}
}
val sshAntTask = configurations.create("sshAntTask")
dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-websockets-jvm:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktor_version")
implementation("io.ktor:ktor-server-call-logging-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-sessions:$ktor_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
implementation("io.ktor:ktor-server-auth-jvm:$ktor_version")
implementation("io.ktor:ktor-server-auth-jwt-jvm:$ktor_version")
// Kmongo
implementation("org.litote.kmongo:kmongo:$kmongo_version")
implementation("org.litote.kmongo:kmongo-coroutine:$kmongo_version")
implementation("commons-codec:commons-codec:$commons_codec_version")
sshAntTask("org.apache.ant:ant-jsch:1.10.12")
}
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
manifest {
attributes(
"Main-Class" to application.mainClass.get()
)
}
}
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 = "Ip"
val key = file("keys/newkey")
val jarFileName = "jarfile"
try {
"scp"(
"file" to file("build/libs/$jarFileName"),
"todir" to "$user@$host:/root/service",
"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/service/$jarFileName /root/service/service.jar"
)
"ssh"(
"host" to host,
"username" to user,
"keyfile" to "$key",
"trust" to true,
"knownhosts" to knownHosts,
"command" to "systemctl stop service"
)
"ssh"(
"host" to host,
"username" to user,
"keyfile" to "$key",
"trust" to true,
"knownhosts" to knownHosts,
"command" to "systemctl start service"
)
} finally {
knownHosts.delete()
}
}
}
}
我尝试过的解决方案:
使用新的 ssh 密钥对。
更改了 sshd_config 配置。
更改文件权限。
检查我是否提供了正确的 ssh 密钥。
更改了依赖版本。