jenkins结合cygwin软件实现从centos发布代码rsync到windows server2019的过程
需求:代码需要通过jenkins发布到windows中,如果通过人工去上传,效率太低,直接使用jenkins发布会提升效率
难点是把jenkins(linux) 中拉取的代码,编译后传输到windows中,在windows中安装cygwin软件启动rsync服务可以解决这个问题
1.下载cygwin这个软件
打开https://cygwin.com/install.html, 针对不同的windows类型下载,本机下载的是(https://cygwin.com/setup-x86_64.exe),点击安装,点击在线安装
要根据提示安装rsync同步和cygrunsrv服务管理这两个插件
2.rsync配置的编辑
# rsync主配置文件
c:/cygwin64/etc/rsyncd.conf
use chroot = yes max connections = 4 log file = /var/log/rsyncd.log [rsync_test] path = /cygdrive/c/rsync_test comment = Rsync Test auth users = rsync_user secrets file = /etc/rsyncd.secrets write only = false read only = false list = true [apache_go.chinasoft.com] path = /cygdrive/c/Users/Administrator/wwwroot/go.chinasoft.com read only = false transfer logging = yes secrets file = /etc/rsyncd.secrets [apache_convert.chinasoft.com] path = /cygdrive/c/Users/Administrator/wwwroot/convert.chinasoft.com read only = false transfer logging = yes secrets file = /etc/rsyncd.secrets
4.编辑rsync的密码
c:/cygwin64/etc/rsyncd.secrets
apache:pass
# 管理员身份运行 cygwin teminal
# cygwin teminal管理员身份授权密码文件
chmod o-rwx /etc/rsyncd.secrets
3.执行一下命令安装rsync服务
cygrunsrv -I "Rsync" -p "c:\cygwin64\bin\rsync.exe" -a "--config=c:\cygwin64\etc\rsyncd.conf --daemon --no-detach" -f "Rsync daemon service" --user Administrator --passwd pass --stdout "c:\cygwin64\rsyncd-stdin.log" --stderr "c:\cygwin64\rsyncd-stderr.log" -y "tcpip" -e "CYGWIN=nontsec binmode" -c "c:\cygwin64" -o
# 启动服务
cygrunsrv.exe -S "Rsync"
# 如果按照失败可以在teminal中删除服务
cygrunsrv.exe -R "Rsync"
# windows防火墙给指定的ip放行873端口
4.jenkins发布代码的配置
# 拉取代码前,推送代码
#!/bin/bash # 此脚本功能为根据构建时选择的参数,同步 /data/www/vhosts/go.chinasoft.com 下的文件同步到远程中转机器 # 2020.0909 初始化脚本 #非apache用户运行脚本,则退出 if [ `whoami` != "apache" ];then echo "only apache can run me" exit 1 fi ## 1.定义变量 dir_name=bak.$(date +%Y-%m-%d-%H-%M-%S) project_dir=/data/www/vhosts/go.chinasoft.com ## 2.备份代码函数 function func_project_backup(){ cp -a $project_dir/ /data/data_backup/go.chinasoft.com_$dir_name } func_project_backup ## 3.判断代码发布目录变量是否为空 if [ ! $project_dir ];then echo "$project_dir IS NULL ,shell exit!!!!" exit 1 fi ## 2.判断同步状态 function func_rsync_status(){ if [[ $? == 0 || $? == 23 ]];then rsync_edit=1 else rsync_edit=0 echo "`date` 同步到本地目标失败! " exit 1 fi } # 执行编译 cd ${WORKSPACE} && chmod +x ./init.sh && ./init.sh production ## 4.同步到本地待发路径 function func_rsync_project_local(){ echo "xxxxxxxxxxxxxx同步待发目录开始xxxxxxxxxxxxxxxxxx" cd $WORKSPACE /usr/local/bin/rsync -vauP -progress --delete --exclude='.git/' --exclude='.gitignore' --exclude='*.log' $WORKSPACE/ $project_dir/ echo "xxxxxxxxxxxxxx同步待发目录完成xxxxxxxxxxxxxxxxxx" func_rsync_status } func_rsync_project_local
func_rsync_project_local
## 5.推送代码到远程中转机并发布(发布到线上)
echo "------------------------------------ rsync start prod -----------------------------------------"
chown -R apache.users $project_dir/
sleep 1
/bin/bash /usr/local/worksh/jeninks_task/media_io_go.chinasoft.com.sh
func_rsync_status
echo "------------------------------------ rsync done prod -----------------------------------------"
## 7.通过插件执行远程中转机上的同步脚本
# 在jenkins中执行同步代码到windows的脚本
/usr/local/worksh/jeninks_task/media_io_go.chinasoft.com.sh
#!/bin/bash ############################################# ## 设置变量和GET请求过来的变量 ## GET请求传过来的文件所在目录,目录路径写全路径了 #dir=$1 passfile="/data/www/.rsync/pass.media_io_win.chinasoft.com" # 非apache用户运行脚本,则退出 if [ `whoami` != "apache" ];then echo " only apache can run me" exit 1 fi # 判断同步状态 function func_rsync_status(){ if [[ $? == 0 || $? == 23 ]];then rsync_edit=1 else rsync_edit=0 echo "`date` 同步到目标失败! " exit 1 fi } # 判断目录是否为空函数 function func_is_empty_dir(){ return `ls -A $1|wc -w` } # 代码发目录 project_dir="/data/www/vhosts/go.chinasoft.com/" # 判断待发目录是否为空,为空则退出 if func_is_empty_dir $project_dir then echo " $project_dir is empty , exit!!!!" exit 1 else echo " $project_dir 可以发布" fi ## 设置变量,目标服务器 server_ip_list="1.1.1.1" # src directory src_directory="go.chinasoft.com" # dst directory dst_directory="go.chinasoft.com" exclude_list="--exclude=.svn --exclude=.git --exclude=.gitignore --exclude=*.log --exclude=.gitattributes --exclude=.env" function account_media_io_rsync() { # rsync ip_list for ip in ${server_ip_list} do echo "####################rsync ${ip} start################################" rsync -zavP --bwlimit=1000 ${exclude_list} --password-file=${passfile} /data/www/vhosts/${src_directory}/ apache@${ip}::apache_go.chinasoft.com func_rsync_status echo "################### rsync ${ip} end #######################" done } account_media_io_rsync
推送代码后置操作,进行远程windows bat脚本进行服务的重启