vue项目自动打包推送。需要npm run install
针对前端VUE版本更新,自动下载、打包、备份、上传全量更新
第一步:点击选择需要部署的城市,会自动生成部署的项目信息,确认IP和项目路径
第二步:点击构建
jobs应用场景
公司代码库利用npm run build:prod打包后传送接口文件夹到目标服务器
目录:DPstatic 文件:data.json favicon.ico index.html
首先打完包后备份部署服务器的源文件到/data/backup目录下,传送新版本文件夹到目标服务器,
一、构建界面
二、指定构建地区
二、构建说明
三、需要部署到的服务器,下面指定构建Referenced parameters为上面的area_name
四、需要部署到目标服务器路径,下面指定构建Referenced parameters为上面的area_name
五、git库
六、构建时输出有时间、颜色、全局变量
七、构建shell
#!/bin/bash
echo -e " 33[34m#####判断是否正确输入数据##### 33[0m"
if [ -z ${source_file} ] && [ -z ${destination_file} ];then
echo -e " 33[31m未输入参数,请重新输入 33[0m"
exit 1
else
echo -e " 33[34m输入内容正确 33[0m"
fi
echo -e " 33[34m######判断IP是否输入正确##### 33[0m"
cat /etc/ansible/hosts | grep -v "#" | awk '{print $1}' | grep ${destination_ip}
if [ $? -eq 0 ]
then
echo -e " 33[34m此IP在hosts列表,可以执行同步 33[0m"
else
echo -e " 33[31m确定IP,先添加到hosts文件 33[0m"
exit 1
fi
echo -e " 33[34m#######打印输入##### 33[0m"
echo -e " 33[34m源路径:${source_file} 33[0m"
echo -e " 33[34m目标路径:${destination_file} 33[0m"
echo -e " 33[34m目标IP:${destination_ip} 33[0m"
if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ]
then
echo -e "版本相同无需打包"
else
echo -e " 33[34m开始执行npm install 33[0m"
#/usr/local/node/bin/npm install -g yarn
#yarn install
cnpm install
sleep 5
[ $? -eq 0 ] && echo -e " 33[32m 执行npm install成功。 33[0m" || {
echo -e " 33[31m执行npm install失败。 33[0m"
exit 1
}
echo -e " 33[34m执行npm run build 33[0m"
nohup /usr/local/node/bin/npm run build:prod &
sleep 40
kill 2
[ $? -eq 0 ] && echo -e " 33[32m 执行npm run build成功。 33[0m" || {
echo -e " 33[31m执行npm run build失败。 33[0m"
exit 1
}
cd ${source_file}
zip -r ${source_filezip} ./ && {
echo -e " 33[32m压缩成功。 33[0m"
} || {
echo -e " 33[32m压缩失败。 33[0m"
exit 1
}
mv ${source_filezip} $WORKSPACE
fi
#####利用ansible进行文件拷贝
date=`date "+%Y-%m-%d-%H-%M-%S"`
echo -e " 33[34m#####目标文件备份##### 33[0m"
echo ${destination_file} > /tmp/name
backup_name=/data/backup/front-"`cat /tmp/name | awk -F "/" '{print $5}'`"/"`cat /tmp/name | awk -F "/" '{print $5}'`".bak${date}
echo -e " 33[34m#判断备份目录是否存在,不存在重新创建 33[0m"
ansible ${destination_ip} -m file -a 'path={{ backup_name }} state=directory' -e backup_name=${backup_name}
echo -e " 33[34m$backup_name备份目录创建完成 33[0m"
echo -e " 33[34m#判断发版目录是否存在,不存在重新创建 33[0m"
ansible ${destination_ip} -m file -a 'path={{ destination_file }} state=directory' -e destination_file=${destination_file}
echo -e " 33[34m$backup_name发版目录创建完成 33[0m"
echo -e " 33[34m###开始备份### 33[0m"
DPstatic=${destination_file}/DPstatic
data_json=${destination_file}/data.json
favicon_ico=${destination_file}/favicon.ico
index_html=${destination_file}/index.html
#back_file=${destination_file##*/}
ansible "$destination_ip" -m command -a 'mv "{{ DPstatic }}" "{{ backup_name }}"' -e DPstatic=${DPstatic} -e backup_name=${backup_name}
[ $? -eq 0 ] && echo -e " 33[34m ${DPstatic}备份完成 33[0m" || {
echo -e " 33[31m${DPstatic}备份失败 33[0m"
exit 1
}
ansible "$destination_ip" -m command -a 'mv "{{ data_json }}" "{{ backup_name }}"' -e data_json=${data_json} -e backup_name=${backup_name}
[ $? -eq 0 ] && echo -e " 33[34m ${data_json}备份完成 33[0m" || {
echo -e " 33[31m${data_json}备份失败 33[0m"
exit 1
}
ansible "$destination_ip" -m command -a 'mv "{{ favicon_ico }}" "{{ backup_name }}"' -e favicon_ico=${favicon_ico} -e backup_name=${backup_name}
[ $? -eq 0 ] && echo -e " 33[34m ${favicon_ico}备份完成 33[0m" || {
echo -e " 33[31m${favicon_ico}备份失败 33[0m"
exit 1
}
ansible "$destination_ip" -m command -a 'mv "{{ index_html }}" "{{ backup_name }}"' -e index_html=${index_html} -e backup_name=${backup_name}
[ $? -eq 0 ] && echo -e " 33[34m ${index_html}备份完成 33[0m" || {
echo -e " 33[31m${index_html}备份失败 33[0m"
exit 1
}
echo -e " 33[34m###开始同步### 33[0m"
ansible "$destination_ip" -m copy -a 'src="{{ source_filezip }}" dest="{{ destination_file }}"' -e source_filezip=${WORKSPACE}/${source_filezip} -e destination_file=${destination_file}
result=$?
[ ${result} -eq 0 ] && echo -e " 33[34m${source_filezip}同步完成! 33[0m" || {
exit $result
echo -e " 33[31m${source_filezip}同步失败! 33[0m"
}
echo -e " 33[34m###开始解压### 33[0m"
ansible "$destination_ip" -m command -a 'unzip -o -d "{{ destination_file }}" "{{ source_filezip }}"' -e source_filezip=${destination_file}/${source_filezip} -e destination_file=${destination_file}
result=$?
[ ${result} -eq 0 ] && echo -e " 33[34m${source_filezip}解压完成! 33[0m" || {
exit $result
echo -e " 33[31m${source_filezip}解压失败! 33[0m"
}
echo -e " 33[34m###目录权限添加### 33[0m"
ansible "$destination_ip" -m command -a 'chmod -R o+r "{{ destination_file }}"' -e destination_file=${destination_file}
result=$?
[ ${result} -eq 0 ] && echo -e " 33[34m${source_filezip}权限添加成功! 33[0m" || {
exit $result
echo -e " 33[31m${source_filezip}权限添加失败! 33[0m"
}



