jenkins备份和回滚
首先在项目配置中的General中:
1.勾选Discard old builds(丢弃旧的构建)
Days to keep builds 代表保存构建的天数,Max# of builds to keep 代表保存构建的最大个数。如图
2.勾选This project is parameterized(参数化构建过程)
添加Choice Parameter(选择参数)和String parameter(文本参数),设置两个参数,status-构建动作,默认值有deploy-发布,rollback-回滚;version-构建记录id值,默认值为0如图:
在Source Code Management(原码)
选好git链接,并填写好分支
在Build(构建)里,添加shell脚本。
脚本如下:
case $status in
deploy) echo "status:$status" backup_path="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/target" deploy_path="/data/wwwroot/znbk.dev.qingtingzy.com/" if [ -d $backup_path ]; then echo "$backup_path is exists!" else mkdir -p $backup_path fi sudo rsync -r -z -P -p -t --delete $deploy_path $backup_path/ sudo rsync -r -z -P -p -t --delete ${WORKSPACE}/ $deploy_path echo "deploy done!" ;; rollback) echo "status:$status" echo "build_id:$version" backup_path="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/$version/target" deploy_path="/data/wwwroot/znbk.dev.qingtingzy.com/" rm -rf $deploy_path cp -r $backup_path/ $deploy_path echo "roll_back done!" ;; *) exit ;;
esac
然后点击保存。
点击Build with Parameters,选择构建动作,deploy部署或rollback回滚,选择rollback则需要输入version构建记录ID,version构建记录ID对应的是构建历史的ID,例如#20的20
然后点击build就可以了。