【问题标题】:How to transfer file from one server to another via SSH Steps Plugin如何通过 SSH 步骤插件将文件从一台服务器传输到另一台服务器
【发布时间】:2020-06-01 10:58:41
【问题描述】:

我是 Jenkins 的新手,我不知道如何通过 SSH 步骤插件将文件从一台服务器传输到另一台服务器。 你能展示一些管道示例吗? 非常感谢您的帮助!

我已经尝试过使用:

def remote = [:]
remote.name = "Nginx"
remote.host = "1.2.3.4"
remote.allowAnyHosts = true

node {
    withCredentials([sshUserPrivateKey(credentialsId: 'Nginx_inst', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'myUser')]) {
        remote.user = myUser
        remote.identityFile = identity
        stage("SSH Transfer") {
        sshCommand remote: remote, command: "sudo cp /home/myUser/docs.zip /var/www/html"
//      sshPut remote: remote, from: 'docs.zip', into: '/var/www/html/'
        sshCommand remote: remote, command: "sudo unzip -tq /var/www/html/docs.zip"
        }
    }
}

### But I take an error:
    Executing command on ****[1.2.3.4]: sudo cp /home/****/docs.zip /var/www/html sudo: false
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
com.jcraft.jsch.JSchException: USERAUTH fail
    at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
    at com.jcraft.jsch.Session.connect(Session.java:470)
..............
Finished: FAILURE

【问题讨论】:

  • 你指的是 Jenkins 的远程到远程文件传输吗?
  • 我的 file.zip 在 Jenkins 服务器 (/var/lib/jenkins/workspace/test-item-xyz/xyz.zip) 上,我正在尝试通过 SSH 将其传输到另一个 Nginx 服务器/var/www/html/ 文件夹中的步骤插件。然后我想解压)
  • 通常,您使用 IP/主机名和登录凭据配置/定义远程主机,并使用脚本配置作业/管道以作为 ssh 执行。既然你已经写好了管道,你会从 Jenkins 控制台发布任何错误日志来查看吗?
  • Running in Durability level: MAX_SURVIVABILITY [Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/copy_unzip [Pipeline] { [Pipeline] withCredentials Masking 支持的模式匹配$identity or $ or $syniuk [Pipeline] { [Pipeline] stage [Pipeline] { (SSH Transfer) [Pipeline] sshPut 发送文件/目录到 ****[52.28.246.156]: from: /var/lib /jenkins/workspace/copy_unzip/docs.zip 到:/var/www/html/ 被 Maxim 中止
  • 我在这里没有看到任何错误,它只是表明你已经“放弃”了执行。此外,请始终通过编辑问题本身来发布这些详细信息。输出或代码在 cmets 中不可读,最好将其作为问题的一部分发布。

标签: jenkins ssh copy jenkins-pipeline jenkins-plugins


【解决方案1】:

这与 SSH 密钥有关。您的错误是因为从 Jenkins 到目标服务器进行 SSH 时使用的密钥存在问题。

看看"com.jcraft.jsch.JSchException: Auth fail" with working passwords

【讨论】:

  • 抱歉难以理解。我的 jenkins 用户可以使用我的 pem 密钥通过终端进入 1.2.3.4。我还将我的 pem 密钥和用户添加到凭据中。在 1.2.3.4 服务器上,我有带有 .ssh/authorized_keys 的 myUser。所以它通过终端连接良好。但为什么它不适用于管道?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多