Linux

在 jenkins 中執行 RSYNC 命令

  • December 22, 2017

我正在嘗試將我的程式碼部署到遠端伺服器。到目前為止我已經嘗試過

  • 通過 SSH 外掛發布:問題是我無法保留文件權限
  • rsync 命令:這會保留文件權限,但問題是我不知道如何設置密碼以使其自動工作

這就是我得到的:

[JenkinsBuild] $ /bin/sh -xe /var/lib/jenkins/tmp/hudson4646064064846581974.sh
+ rsync -PSauve ssh --exclude=JenkinsBuild app bower.json config gruntfile.js karma.conf.js LICENSE.md Makefile node_modules package.json Procfile protractor.conf.js public README README.md server.js john@192.168.2.10:/srv/dp/prod
Host key verification failed.
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]

有什麼想法我在這裡做錯了嗎?

好吧,我替換了 know_host 文件,但我仍然遇到問題。因此,對於任何對 know_host 文件有問題的人,您可以通過執行以下操作來修復有問題的密鑰:

ssh-keygen -R hostnameOfOfendingKey

這將刪除 offending 密鑰,現在您的 known_host 文件再次工作。

現在要解決其他問題,我在其他地方找到了答案:https ://stackoverflow.com/questions/25755418/executing-rsync-in-jenkins/25770519#25770519

這就是我為解決它所做的

sudo su jenkins -s /bin/bash

然後作為 jenkins 使用者將我的 ssh 密鑰複製到我想要連接/執行命令的伺服器等

ssh-copy-id myuser@TheRemoteserver.com

然後它會給你這個

myuser@TheRemoteserver.com's password: 
Now try logging into the machine, with "ssh 'myuser@TheRemoteserver.com'", and check in:

 .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

然後我出於測試目的這樣做:

bash-4.1$ rsync -PSauve ssh --exclude=JenkinsBuild /srv/pd/* myuser@TheRemoteserver.com:/srv/pdc/test1  

它沒有要求密碼或任何東西就通過了。請注意,我仍然以詹金斯的身份登錄。

因此,現在當我使用一些 shell 命令作為建構的一部分創建 Jenkins 作業時,它將按預期工作:D

Host key verification failed.

執行 jenkins 的帳戶可能對您要訪問的機器的主機密鑰有錯誤的值~/.ssh/known_hosts.

引用自:https://serverfault.com/questions/627552