createDeploySh.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const fs = require("fs");
  2. const path = require("path");
  3. const dirs = fs.readdirSync(path.resolve(__dirname, "apps"));
  4. for (const d of dirs) {
  5. const config = require(path.resolve(__dirname, `apps/${d}/deploy.config`));
  6. let content = `#!/bin/bash
  7. #
  8. # 部署脚本
  9. #
  10. set -e
  11. #
  12. APP_NAME=${config.appName}
  13. START_TIME=$(date +%s)
  14. DATE=$(date +%m/%d/%y)
  15. TIME=$(date +%k:%M:%S)
  16. TARGET_VERSION=$(date +%Y%m%d%H%M)
  17. # 目录配置
  18. DIST_NAME="dist"
  19. BUILD_NAME=${config.buildName}
  20. WORK_DIR=${config.workDir}
  21. REMOTE_DIR=${config.remoteDir}
  22. # SSH配置
  23. REMOTE_USER=${config.remoteUser}
  24. REMOTE_IP=${config.remoteIp}
  25. PRIVATE_SSH_KEY=${config.privateSSHKey}
  26. # workspace
  27. cd $WORK_DIR
  28. # build
  29. echo -e "\\033[34m[INFO]\\033[0m Building $APP_NAME ..."
  30. npm run $BUILD_NAME
  31. # upload
  32. echo -e "\\033[34m[INFO]\\033[0m Uploading $APP_NAME to $REMOTE_IP ..."
  33. tar -cf "$DIST_NAME.zip" $DIST_NAME
  34. scp -i $PRIVATE_SSH_KEY "$DIST_NAME.zip" "$REMOTE_USER@$REMOTE_IP:$REMOTE_DIR"
  35. rm "$DIST_NAME.zip"
  36. # publish
  37. echo -e "\\033[34m[INFO]\\033[0m Publishing $APP_NAME to $REMOTE_IP ..."
  38. ssh -i $PRIVATE_SSH_KEY "$REMOTE_USER@$REMOTE_IP" /bin/bash <<EOF
  39. cd $REMOTE_DIR
  40. tar -xf "$DIST_NAME.zip" && cp -r $DIST_NAME/* ./
  41. mkdir -p "archive"
  42. mv $DIST_NAME $TARGET_VERSION && mv $TARGET_VERSION "archive"
  43. ls -l index.html
  44. # rm "$DIST_NAME.zip"
  45. exit
  46. EOF
  47. END_TIME=$(date +%s)
  48. EXECUTION_TIME=$((END_TIME - START_TIME))
  49. echo "脚本执行时间:$EXECUTION_TIME 秒".
  50. echo -e "\\033[32m[DONE]\\033[0m Deployed complete."`;
  51. // 写入部署脚本文件
  52. const file = path.resolve(__dirname, `apps/${d}/deploy.sh`);
  53. console.log(`写入 ${d} 部署脚本文件。`);
  54. fs.writeFileSync(file, content);
  55. }