123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- const fs = require("fs");
- const path = require("path");
- const dirs = fs.readdirSync(path.resolve(__dirname, "apps"));
- for (const d of dirs) {
- const config = require(path.resolve(__dirname, `apps/${d}/deploy.config`));
- let content = `#!/bin/bash
- #
- # 部署脚本
- #
- set -e
- #
- APP_NAME=${config.appName}
- START_TIME=$(date +%s)
- DATE=$(date +%m/%d/%y)
- TIME=$(date +%k:%M:%S)
- TARGET_VERSION=$(date +%Y%m%d%H%M)
- # 目录配置
- DIST_NAME="dist"
- BUILD_NAME=${config.buildName}
- WORK_DIR=${config.workDir}
- REMOTE_DIR=${config.remoteDir}
- # SSH配置
- REMOTE_USER=${config.remoteUser}
- REMOTE_IP=${config.remoteIp}
- PRIVATE_SSH_KEY=${config.privateSSHKey}
- # workspace
- cd $WORK_DIR
- # build
- echo -e "\\033[34m[INFO]\\033[0m Building $APP_NAME ..."
- npm run $BUILD_NAME
- # upload
- echo -e "\\033[34m[INFO]\\033[0m Uploading $APP_NAME to $REMOTE_IP ..."
- tar -cf "$DIST_NAME.zip" $DIST_NAME
- scp -i $PRIVATE_SSH_KEY "$DIST_NAME.zip" "$REMOTE_USER@$REMOTE_IP:$REMOTE_DIR"
- rm "$DIST_NAME.zip"
- # publish
- echo -e "\\033[34m[INFO]\\033[0m Publishing $APP_NAME to $REMOTE_IP ..."
- ssh -i $PRIVATE_SSH_KEY "$REMOTE_USER@$REMOTE_IP" /bin/bash <<EOF
- cd $REMOTE_DIR
- tar -xf "$DIST_NAME.zip" && cp -r $DIST_NAME/* ./
- mkdir -p "archive"
- mv $DIST_NAME $TARGET_VERSION && mv $TARGET_VERSION "archive"
- ls -l index.html
- # rm "$DIST_NAME.zip"
- exit
- EOF
- END_TIME=$(date +%s)
- EXECUTION_TIME=$((END_TIME - START_TIME))
- echo "脚本执行时间:$EXECUTION_TIME 秒".
- echo -e "\\033[32m[DONE]\\033[0m Deployed complete."`;
- // 写入部署脚本文件
- const file = path.resolve(__dirname, `apps/${d}/deploy.sh`);
- console.log(`写入 ${d} 部署脚本文件。`);
- fs.writeFileSync(file, content);
- }
|