jenkinsfile 使用 sshUserPrivateKey 获取身份验证失败错误

问题描述 投票:0回答:1

在jenkins文件中,我想使用私钥连接服务器,但出现身份验证失败错误。

def remoteConfig = [:]
remoteConfig.name = "my-remote-server"
remoteConfig.host = "${REMOTE_HOST}"
remoteConfig.allowAnyHosts = true

node {
  withCredentials([sshUserPrivateKey(
    credentialsId: "${REMOTE_CRED}",
    keyFileVariable: "privateKeyFilePath",
    passphraseVariable: '', usernameVariable: 'rocky'
  )]) {

    remoteConfig.user = "${REMOTE_USER_NAME}"
    remoteConfig.identityFile = privateKeyFilePath

    stage("ssh") {

    
        sh """
            ssh \
              -p 22 \
              -i ${privateKeyFilePath} \
              ${REMOTE_USER_NAME}@${REMOTE_HOST} \
              'echo Hello World!'
          """

      sshCommand remote: remoteConfig, command: 'for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done'

    }
  }
}



但出现此错误

com.jcraft.jsch.JSchException: Auth fail
    at com.jcraft.jsch.Session.connect(Session.java:519)

似乎 sshCommand 有 bug,但不确定

jenkins-pipeline jenkins-plugins
1个回答
0
投票

我认为问题可能是您想使用存储在磁盘上某个路径中的私钥,但在您的脚本中您使用

withCredentials
从 jenkins 存储中读取私钥。

如果您的私钥位于路径文件中,请不要使用

withCredentials
,而是尝试使用
identityFile
指定私钥文件路径,使用
passphrase
指定
remote
中的私钥文件密码,如文档中所定义https://github.com/jenkinsci/ssh-steps-plugin

node {
  def remoteConfig = [:]
  remoteConfig.name = 'my-remote-server'
  remoteConfig.host = "${REMOTE_HOST}"
  remoteConfig.allowAnyHosts = true
  remoteConfig.user = "${REMOTE_USER_NAME}"
  remoteConfig.identityFile = privateKeyFilePath
  remoteConfig.passphrase = 'privateKeyPassword'
  stage('Remote SSH') {
    sshCommand remote: remoteConfig, command: 'for i in {1..5}; do echo -n "Loop \$i"; date ; sleep 1; done'

  }
}

© www.soinside.com 2019 - 2024. All rights reserved.