前提:
因为公司内部测试环境打包都是先在本地代码打成.zip包后发布到测试环境测试通过后才提交svn代码,所以想要实现自动化构建还有一些距离。我的想法是通过开放一个ftp服务,请开发人员将zip包上传到指定位置后,通过crontab定时执行(每10min)去探测目标位置是否有最新安装包,如果有就去触发jenkins的job,此job就是将zip包发送到部署机器上并执行部署脚本。
将zip包发送到部署机器而不是直接上传到部署机器上考虑到的是减少部署机器的端口开放保证安全,只将zip包上传到内网机器上,因为部署多个环境,需要判断1/2/3/4/5/6/7/8分别对应那个项目并进行部署。
构建jenkins的job
1.新建任务
image.png
2.全局变量中配置
image.png
3.远程触发构建
image.png
curl -u username:passwd -X post http://xx.xx.xx.xx:8080/jenkins/job/5pao_test_Deploy_errorEmail/build?token=deploy
执行上面的命令就可以远程构建job,注意更改username,passwd,和token名称
如果只是向远程机器发送文件,这一步就可以了,下面的是根据公司内部环境编写的部署脚本
脚本部署
ftp上传的文件要有规范,必须是1_xxxx.zip,2_xxxx.zip等等
jenkins机器中判断ftp文件目录是否已经上传文件的脚本,如果格式正确触发部署job,格式不正确触发发送邮件告知job
#!/usr/bin/env bash
#judge project 保证TARGET目录只有一个部署文件
#author:daisy
#date:20190519
TARGET=/tomcat/.jenkins/workspace/5paoTest_Deploy
SCRIPT=/data/5pao_deploy_package/backup
PROJECT=`ls $TARGET >$SCRIPT/project.txt`
PROJECT_VERSION=`cat $SCRIPT/project.txt|cut -f1 -d "_"`
COUNT=`ls $TARGET|wc -l`
date=`date +"%Y%m%d-%H%M"`
function wpao_deploy(){
##判断格式是否正确
case $PROJECT_VERSION in
'')
curl -u xxx:xxx -X post http://192.168.1.xxx:8080/jenkins/job/5pao_test_Deploy_errorEmail/build?token=errorEmail
printf 'upload file failed\n';;
*[0-9]*)
##格式正确调用jenkins流程
curl -u xxx:xxx -X post http://192.168.1.xxx:8080/jenkins/job/5paoTest_Deploy/build?token=deploy
printf 'Successful deployment file\n';;
*)
curl -u xxxx:xxxx-X post http://192.168.1.xxx:8080/jenkins/job/5pao_test_Deploy_errorEmail/build?token=errorEmail
printf 'It seems that there are some problems\n';;
esac >&2
}
##如果只有一个文件,去部署,如果是0个或者多个,触发格式错误
if [ $COUNT == 1 ]; then
##调用函数
wpao_deploy
echo "start deloy....\n"
sleep 15
##备份
if [ ! -d $SCRIPT/$date ]; then
mkdir -p $SCRIPT/$date
fi
mv $TARGET/* $SCRIPT/$date
echo "move package success!"
else
curl -u xxx:xxxx -X post http://192.168.1.xxx:8080/jenkins/job/5pao_test_Deploy_errorEmail/build?token=errorEmail
echo "target file seems has some problems\n"
fi
crontab中每天九点到下午九点,每十分钟去探测ftp目录是否有文件的配置
*/10 09-21 * * * /bin/bash /data/5pao_deploy_package/5pao_crontab_deploy.sh > /dev/null 2>&1
部署机器上判断是哪个环境
#!/usr/bin/env bash
#judge project 保证TARGET目录只有一个部署文件
#author:daisy
#date:20190519
TARGET=/home/adminis/deploy/target
SCRIPT=/home/adminis/deploy/scripts
PROJECT=`ls $TARGET >$SCRIPT/project.txt`
PROJECT_VERSION=`cat $SCRIPT/project.txt|cut -f1 -d "_"`
case $PROJECT_VERSION in
'')
printf 'upload file failed\n';;
*[0-9]*)
#sh $SCRIPT/deploy.sh $PROJECT_VERSION
printf 'Successful deployment file\n';;
*)
printf 'It seems that there are some problems\n';;
esac >&2
部署脚本
网友评论