【问题标题】:SSH from jenkins to same host从詹金斯到同一主机的SSH
【发布时间】:2018-07-01 05:00:12
【问题描述】:

我有一个 jenkins 实例在我的树莓派 3 上运行,我的(简单)apache 网络服务器也在同一个树莓派上运行。

我有一个来自 jenkins 的管道来获取一个 git repo,构建它并(通过 scp)将构建文件放在我的网络服务器上。

我有一个 ssh 私钥/公钥设置,但是当 jenkins 托管在具有相同 IP 地址的同一“机器”上时,拥有一个 ssh 密钥有点愚蠢(?)?

无论如何,在我的树莓派上,我已经设置了自动密钥文件和带有公钥的已知主机文件,并且我已经通过 ssh-agent 插件将私钥添加到 jenkins。

这里有我的 jenkins 文件,jenkins 使用它来定义我的管道:

node{
    stage('Checkout') {
        checkout scm
    }

    stage('install') {
        nodejs(nodeJSInstallationName: 'nodeJS10.5.0') {
            sh "npm install"
        }
    }

    stage('build'){
        nodejs(nodeJSInstallationName: 'nodeJS10.5.0') {
            sh "npm run build"
        }
    }

    stage('connect ssh and remove files') {
        sshagent (credentials: ["0527982f-7794-45d0-99b0-135c868c5b36"]) {
            sh "ssh pi@123.456.789.123 -p 330 rm -rf /var/www/html/*"
        }
    }


    stage('upload new files'){
        sshagent (credentials: ["0527982f-7794-45d0-99b0-135c868c5b36"]) {
            sh "scp -P 330 -r ./build/* pi@123.456.789.123:/var/www/html"
        }
    }
}

这是失败的倒数第二个作业的输出:

[Pipeline] }
[Pipeline] // nodejs
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (connect ssh and remove files)
[Pipeline] sh
[Deploy_To_Production] Running shell script
+ ssh pi@123.456.789.123 -p 330 rm -rf /var/www/html/asset-manifest.json /var/www/html/css /var/www/html/favicon.ico /var/www/html/fonts /var/www/html/images /var/www/html/index.html /var/www/html/manifest.json /var/www/html/service-worker.js /var/www/html/static /var/www/html/vendor
Host key verification failed.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE

注意:出于安全原因,我更改了 IP 地址和 ssh 端口。

我可以手动 ssh 到我的树莓派,我可以从我的笔记本电脑手动执行命令(来自同一个域和其他域的作品)。

我还端口转发了本地 ip,以便我不在家时通过 SSH 连接到它。

我猜我在 SSH 密钥等方面做错了,但我不是专家!

谁能帮忙?

【问题讨论】:

  • 为了以后参考,这里最好使用rsync

标签: jenkins ssh jenkins-pipeline


【解决方案1】:

我还需要4个声望点来评论,所以我必须写答案:)

尝试使用-v调试ssh连接:

stage('connect ssh and remove files') {
    sshagent (credentials: ["0527982f-7794-45d0-99b0-135c868c5b36"]) {
        sh "ssh -v pi@123.456.789.123 -p 330 rm -rf /var/www/html/*"
    }
}

另一方面 主机密钥验证失败表示远程主机的主机密钥已更改或您没有远程主机的主机密钥。因此,首先尝试使用来自 Jenkins 主机的 Jenkins 用户 ssh -v pi@123.456.789.123

【讨论】:

    【解决方案2】:

    问题确实是主机密钥验证失败。我认为这是由于不信任主机。

    但@3sky 指出了真正的问题(请参阅其他答案)。我需要以 jenkins 用户身份登录并尝试通过 ssh 连接到我的树莓派(它们都在同一台机器上)。

    所以这些是我做的步骤:

    1. 通过 ssh 登录到我的树莓派

      ssh -v pi@123.456.789.123 -p 330

    2. 然后我将用户切换到 jenkins 用户。经过一些谷歌搜索,我发现了如何

      sudo su -s /bin/bash jenkins

    3. 然后我再次 ssh 到我自己的机器(我已经 ssh'ed 进去了),这样我就可以一劳永逸地弹出这个主机了!

      ssh -v pi@123.456.789.123 -p 330

    这解决了我的问题!非常感谢 3sky 的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2012-12-23
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多