JENKINS自动部署源代码到远程服务器(四)

拷贝构建文件到远程服务器

Publish Over SSH插件:基于SSH发送文件并在远程服务器上执行命令。
配置SSH密钥对实现免交互认证:
1、生成密钥对:ssh-keygen
2、将id_rsa.pub公钥内容添加到目标服务器(ssh-copy-id)
3、将id_rsa私钥保存到插件配置里
先安装插件

 

 

[root@linux-node1 ~]# ssh-keygen
[root@linux-node1 ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@linux-node1 ~]# ssh-copy-id root@192.168.230.131  #copy到指定服务器web
[root@linux-node2 ~]# ls .ssh/authorized_keys 
.ssh/authorized_keys

验证:使用私钥连接交互登陆

[root@linux-node1 ~]# ssh -i .ssh/id_rsa root@192.168.230.131

 3、将id_rsa私钥保存到插件配置里

[root@linux-node1 ~]# cat .ssh/id_rsa

系统配置

• Source files:传输的源文件,相对路径
• Remove prefix:去除拷贝的源文件上级目录
• Remote directory:拷贝到远程服务器的目录,
与系统配置里的SSH配置的Remote Directory拼接,例如
这里写test,最终目标目标是/tmp/test
• Exec command:在远程服务器执行的Shell脚本

构建

 node2节点查看

[root@linux-node2 ~]# ls /tmp/test/
index.html  README.md

部署测试网站

[root@linux-node2 ~]# rpm -Uvh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@linux-node2 ~]# yum -y install nginx
[root@linux-node2 ~]# cat /etc/nginx/conf.d/default.conf |grep 88  #监听端口88
    listen       88;
[root@linux-node2 ~]# nginx #启动

[root@linux-node2 ~]# ls /usr/share/nginx/html/ #nginx yum安装默认根目录
50x.html  index.html

 将网站目录改成nginx目录

 然后将系统配置ssh里的目录改成/

 构建

在远程服务器执行Shell命令

 安装ssh插件

 

 在系统配置里添加ssh的远程主机,然后再构建项目里进行配置,添加key

 

 控制台输出:

 

 

posted @ 2022-06-09 11:27  w787815  阅读(384)  评论(0编辑  收藏  举报