[root@app-01 ~]# tree -d -L 3 /data/apps/project/ /data/apps/project/ ├── apps │ └── mini-games │ └── public_v1.0.0 └── mini-games -> /data/apps/project/apps/mini-games/public_v1.0.0
#!/bin/bash #Author 1304995320@qq.com project_version=$2 project_name=$3 project_link_path=/data/apps/project project_path=${project_link_path}/apps jenkins_project_path=/data/jenkins/jenkins_task/${project_name} build_name=public list_ip=(172.16.1.43 172.16.1.205) #build代码 function build() { cd ${jenkins_project_path} git clone git@gitlab.example.com:project/mini-games.git git checkout ${project_version} npm install --unsafe-perm npm run build } # 拷贝文件 function scp_tarfile() { for node in ${list_ip[@]};do #判断目录是否存在,不存在创建 ssh root@${node} "[ ! -d ${project_path}/${project_name} ] && mkdir -pv ${project_path}/${project_name}" #sync更新包 rsync -azP -e 'ssh ' ${jenkins_project_path}/${build_name}/ root@${node}:${project_path}/${project_name}/${build_name}_$project_version #更新软连接 ssh root@${node} "ln -svfn ${project_path}/${project_name}/${build_name}_$project_version ${project_link_path}/${project_name}" echo "${node}节点版本更新成功" done } # 删除节点历史更新包 function delete_history_version() { for node in "${list_ip[@]}";do version_numbers=`ssh root@${node} "ls -ldrt ${project_path}/${project_name}/* | wc -l"` echo " ${node}节点历史更新包版本有${version_numbers} 个" if [ ${version_numbers} -gt 5 ];then echo "即将在${node}节点删除历史更新包 " version_name=`ssh root@${node} "ls -ldrt ${project_path}/${project_name}/* |head -n 1 | awk '{print $9}'"` ssh root@${node} "rm -rf ${version_name}" echo "${node}删除历史版本${version_name} 成功" fi done } # 版本回滚 function rollback_last_version() { for node in "${list_ip[@]}";do echo "当前节点${node}" now_version=`ssh root@${node} ""ls -ldrt ${project_link_path}/${project_name} | awk -F"->" '{print $2}'""` now_version=`basename ${now_version}` echo "${node}当前的版本是----> ${now_version}" rollback_version=`ssh root@${node} ""ls -ldrt ${project_path}/${project_name}* | grep -B1 ${now_version} | head -n1 | awk '{print $9}'""` echo ${rollback_version} ssh root@${node} "ln -svfn ${rollback_version} ${project_link_path}/${project_name}" done } # 站点更新测试 #function web_test() {} # 禁用负载均衡 #function delect_node_lvs() {} # 开启负载均衡 #function add_node_lvs() {} main() { case $1 in Deploy) build scp_tarfile delete_history_version ;; Rollback) rollback_last_version ;; esac } main $1