build.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. #本地压缩文件名
  3. file_name="hac.zip"
  4. #测试服器上的目录地址
  5. file_path="hac.hczcxx.cn"
  6. #测试服务器上的演示地址
  7. demo_url="http://192.168.0.109:5176/"
  8. #测试服务器上的登录密码
  9. passwd="admin123@"
  10. # 打包前的准备
  11. node ./scripts/build.js
  12. # 执行打包命令
  13. vite build
  14. # 恢复配置文件
  15. node ./scripts/restore.js
  16. # 排除打包文件
  17. if [ $1 == "wgt" ]; then
  18. node ./scripts/public.js
  19. fi
  20. # 执行打包为zip
  21. node ./scripts/zip.js
  22. current_time=$(date "+%Y%m%d%H%M%S")
  23. # 上传到测试服务器
  24. function testServer() {
  25. expect -c "
  26. spawn scp ./zip/${file_name} root@192.168.0.109:/www/wwwroot/${file_path}
  27. expect {
  28. \"yes/no\" {send \"yes\r\";exp_continue;}
  29. \"*password\" {set timeout 500;send \"${passwd}\r\";}
  30. }
  31. expect eof"
  32. #服务器上的相关操作
  33. /usr/bin/expect << EOF
  34. set time 30
  35. spawn ssh root@192.168.0.109
  36. expect {
  37. "*yes/no" { send "yes\r"; exp_continue }
  38. "*password:" { send "${passwd}\r" }
  39. }
  40. # 进入当前项目的目录
  41. expect "*]#"
  42. send "cd /www/wwwroot/${file_path}\r"
  43. # 删除 static 目录
  44. expect "*]#"
  45. send "rm -rf static\r"
  46. # 解压上传的文件压缩包
  47. expect "*]#"
  48. send "unzip -o ${file_name}\r"
  49. # 备份上一次的压缩文件
  50. expect "*]#"
  51. send "mv ./backup/${file_name} ./backup/${current_time}_${file_name}\r"
  52. # 执行清理备份文件脚本
  53. expect "*]#"
  54. send "node ./backup.js\r"
  55. # 移动当前文件到备份目录
  56. expect "*]#"
  57. send "mv ./${file_name} ./backup/${file_name}\r"
  58. # 退出
  59. send "exit\r"
  60. expect eof
  61. EOF
  62. echo "编译打包后自动部署到测试服务器上完成"
  63. echo "测试服务器地址:${demo_url}"
  64. }
  65. # 上传到测试服务器
  66. if [ $2 == "test" ]; then
  67. testServer
  68. else
  69. echo "编译完成"
  70. fi