jenkins 备份和回滚(by:自己查资料学习)
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/www.test.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/www.test.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就可以了。
=====================分割线==========================
上面的方法用的是项目和jenkins系统在同一个服务器的脚本,
如果使用远程部署,就是项目和jenkins系统不在一个服务器下,要使用远程的方式,如下:
1.生成秘钥对:
在jenkins所在的服务器生成密钥对,首先要查看jenkins的配置文件,看一下jenkins的权限是在jenkins下,还是root下。然后在对应的权限文件夹内,生成密钥对。
(1)jenkins下:
在jenkins所在的服务器切换到 jenkins账户:su - jenkins
创建密钥:ssh-keygen
生成后得到密钥对位置:
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub
将公钥内容 id_rsa.pub 拷贝到客户端 /root/.ssh/authorized_keys 文件内
(2)root下:
在jenkins所在的服务器切换到 root账户:su - root
创建密钥:ssh-keygen
生成后得到密钥对位置:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
将公钥内容 id_rsa.pub 拷贝到客户端 /root/.ssh/authorized_keys 文件内。