jenkins结合cygwin软件实现从centos发布代码rsync到windows server2019的过程

jenkins结合cygwin软件实现从centos发布代码rsync到windows server2019的过程

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 = yesmax connections = 4log file = /var/log/rsyncd.log[rsync_test]path = /cygdrive/c/rsync_testcomment = Rsync Testauth users = rsync_usersecrets file = /etc/rsyncd.secretswrite only = falseread only = falselist = true [apache_go.chinasoft.com]path = /cygdrive/c/Users/Administrator/wwwroot/go.chinasoft.comread only = falsetransfer logging = yessecrets file = /etc/rsyncd.secrets[apache_convert.chinasoft.com]path = /cygdrive/c/Users/Administrator/wwwroot/convert.chinasoft.comread only = falsetransfer logging = yessecrets 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" ];thenecho "only apache can run me"exit 1fi## 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 1fi## 2.判断同步状态function func_rsync_status(){if [[ $? == 0 || $? == 23 ]];then rsync_edit=1else rsync_edit=0 echo "`date` 同步到本地目标失败! " exit 1fi}# 执行编译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" ];thenecho " only apache can run me"exit 1fi# 判断同步状态function func_rsync_status(){if [[ $? == 0 || $? == 23 ]];then rsync_edit=1else rsync_edit=0 echo "`date` 同步到目标失败! " exit 1fi}# 判断目录是否为空函数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_dirthen echo " $project_dir is empty , exit!!!!" exit 1else echo " $project_dir 可以发布" fi## 设置变量,目标服务器server_ip_list="1.1.1.1"# src directorysrc_directory="go.chinasoft.com"# dst directorydst_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_listfor 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脚本进行服务的重启

技术图片

相关文章