build.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # 上传到测试服务器
  23. function testServer() {
  24. expect -c "
  25. spawn scp ./zip/${file_name} root@192.168.0.109:/www/wwwroot/${file_path}
  26. expect {
  27. \"yes/no\" {send \"yes\r\";exp_continue;}
  28. \"*password\" {set timeout 500;send \"${passwd}\r\";}
  29. }
  30. expect eof"
  31. #服务器上的相关操作
  32. /usr/bin/expect << EOF
  33. set time 30
  34. spawn ssh root@192.168.0.109
  35. expect {
  36. "*yes/no" { send "yes\r"; exp_continue }
  37. "*password:" { send "${passwd}\r" }
  38. }
  39. # 进入当前项目的目录
  40. expect "*]#"
  41. send "cd /www/wwwroot/${file_path}\r"
  42. # 删除 static 目录
  43. expect "*]#"
  44. send "rm -rf static\r"
  45. # 解压上传的文件压缩包
  46. expect "*]#"
  47. send "unzip -o ${file_name}\r"
  48. # 删除上传的文件压缩包
  49. expect "*]#"
  50. send "rm -rf ${file_name}\r"
  51. expect "*]#"
  52. # 退出
  53. send "exit\r"
  54. expect eof
  55. EOF
  56. echo "编译打包后自动部署到测试服务器上完成"
  57. echo "测试服务器地址:${demo_url}"
  58. }
  59. # 上传到测试服务器
  60. if [ $2 == "test" ]; then
  61. testServer
  62. elif [ $2 == "prod" ]; then
  63. echo "暂不支持自动部署到正式服务器上,如有需求,请手动上传到正式服务器上更新"
  64. else
  65. echo "编译完成"
  66. fi