如何在Jenkins中使用rsync、scp远程同步文件

使用Jenkins进行自动构建时,经常有远程同步文件到其他主机的需求,比如上传构建好的程序包到服务器。为实现此需求,我们可以在Jenkins项目中增加一个Execute Shell步骤,然后输入以下Command:
rsync -e ssh --progress /var/www/index.htm username@host.domain.com:/var/www/

username: host.domain.com上的用户名,需要对/var/www/有读写权限;
host.domain.com:你的主机,可以是域名或者IP地址。

如果host.domain.com的ssh端口不是默认端口,比如是2222,则需要给ssh加上-p参数,如下所示:
rsync -e 'ssh -p2222' --progress /var/www/index.htm username@host.domain.com:/var/www/

保存Jenkins项目,开始构建,会发现上面增加的步骤会导致构建失败,在Jenkins控制台可以看到如下错误:

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-45/rsync/io.c(453) [sender=2.6.9]
Build step 'Execute shell' marked build as failure
Finished: FAILURE

继续阅读如何在Jenkins中使用rsync、scp远程同步文件

阅读:645