Jenkins系列-Jenkins通过Publish over SSH插件实现远程部署

配置ssh免秘钥登录                                                                                  

安装Publish over SSH插件

插件使用官网:https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin

主界面——>系统管理——>管理插件——>可选插件——>右上角过滤框中输入“Publish over SSH”——>勾选安装

 

插件配置

主界面——>系统管理——>系统设置——>Publish over SSH

 

参数说明

  • Passphrase: 密码(目标机器的密码)
  • Path to key:key文件(私钥)的路径
  • Key:将私钥复制到这个框中
  • Disable exec:禁止运行命令

私有配置

  • SSH Server Name: 标识的名字(随便你取什么)
  • Hostname: 需要连接ssh的主机名或ip地址,此处填写应用服务器IP(建议ip)
  • Username: 用户名
  • Remote Directory: 远程目录(要发布的目录,比如/usr/local/tomcat/webapps/)
  • Use password authentication, or use a different key:可以替换公共配置(选中展开的就是公共配置的东西,这样做扩展性很好)

私有配置的高级

  • Port:端口(默认22)
  • Timeout (ms):超时时间(毫秒)默认即可
  • Disable exec:禁止运行命令

配置完成后,可以 通过 “Test Configuration” 测试是否配置正确。

构建配置

点击构建的项目—>配置–>构建后操作,选择Send build artifacts over SSH,进行如下配置(注意:下面的Exec command是远程机器上执行的脚本):

脚本内容如下:

#!/bin/bash
echo "==================start deploy project========================="
ps -aux | grep /usr/local/tomcat/ | grep -v grep > tomcat_info.txt
sed 's/ / /g' tomcat_info.txt > tomcat_info1.txt
sed 's/ / /g' tomcat_info1.txt > tomcat_info.txt
sed 's/ / /g' tomcat_info.txt > tomcat_info1.txt 
cat tomcat_info1.txt | cut -d' ' -f 2 > tomcat_info.txt

for line in `cat tomcat_info.txt`
do
kill -9 $line
done

cd /usr/local/tomcat/webapps
rm -rf youxuan_api*
mv /usr/local/jenkinsTempFolder/youxuan_api /usr/local/tomcat/webapps

/etc/init.d/tomcat start
sync
echo 3 > /proc/sys/vm/drop_caches
echo "==================deploy project success========================="

参数说明

  • SSH Server Name:选个一个你在系统设置里配置的配置的名字
  • Transfer Set Source files:需要上传的文件(注意:相对于工作区的路径。看后面的配置可以填写多个,默认用,分隔)
  • Remove prefix:移除目录(只能指定Transfer Set Source files中的目录)
  • Remote directory:远程目录(根据你的需求填写吧,因为我这儿是测试,所以偷懒没有填写。默认会继承系统配置)
  • Exec command:把你要执行的命令写在里面 (新版本的要求Transfer Set Source files和Exec command都要填写,可以不了他。老版本的允许只填写一个)

高级:

  • Exclude files:排除的文件(在你传输目录的时候很有用,使用通配符,例如:**/*.log,**/*.tmp,.git/)
  • Pattern separator:分隔符(配置Transfer Set Source files的分隔符。如果你这儿更改了,上面的内容也需要更改)
  • No default excludes:禁止默认的排除规则(具体的自己看帮助)
  • Make empty dirs:此选项会更改插件的默认行为。默认行为是匹配该文件是否存在,如果存在则创建目录存放。选中此选项会直接创建一个目录存放文件,即使是空目录。(个人理解)
  • Flatten files:只上传文件,不创建目录(除了远程目录)
  • Remote directory is a date format:远程目录建立带日期的文件夹(需要在Remote directory中配置日期格式),具体格式参考下表:
  • Remote directory Directories created
  • 'qa-approved/'yyyyMMddHHmmss qa-approved/20101107154555
  • 'builds/'yyyy/MM/dd/'build-${BUILD_NUMBER}' builds/2010/11/07/build-456 (if the build was number 456)
  • yyyy_MM/'build'-EEE-d-HHmmss 2010_11/build-Sun-7-154555
  • yyyy-MM-dd_HH-mm-ss 2010-11-07_15-45-55
  • Exec timeout (ms):运行脚步的超时时间(毫秒)
  • Exec in pty:模拟一个终端执行脚步
  • Add Transfer Set:增加一个配置
原文地址:https://www.cnblogs.com/zhuochong/p/10082610.html