Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我提供自动部署启动的shell脚本, 使用pm2 #384

Closed
mouhao opened this issue May 31, 2024 · 4 comments
Closed

我提供自动部署启动的shell脚本, 使用pm2 #384

mouhao opened this issue May 31, 2024 · 4 comments

Comments

@mouhao
Copy link

mouhao commented May 31, 2024

.env什么的,自己配置好.

先安装

npm install -g pnpm
npm install -g rimraf
npm install -g pm2

脚本代码

#!/bin/bash

#################################################################
## 作者:huanggh
## 创建时间:2024-04-07 19:48
## 描述:部署文件
## 版本:v1.0
## 语法:./deploy_gpt.sh  {start|stop|restart}
## 参数:
##          please input:
##          start                       启动服务
##          stop                        停止服务
##          restart                     重启服务
##        --help                      显示帮助信息。
## 功能:
##      1. 启动服务
##      2. 停止服务
##      3. 重启服务
## 先安装
## npm install -g pnpm
## npm install -g rimraf
## npm install -g pm2
#################################################################


#当前工程目录
# shellcheck disable=SC2046
PROJECT_HOME=$(echo `pwd`)

#部署目录
DEPLOY_HOME=${PROJECT_HOME}/../../static/chatgpt-web-midjourney-proxy/

mkdir -p ${DEPLOY_HOME}


#检查node是否安装,以及版本是否大于v16
checkNode() {

  #检查node是否安装
  node -v
  if [ $? -ne 0 ]; then
    echo "node is not installed, please install node first."
    exit 1
  fi

  #检查node版本是否大于16.x.x, node -v输出的结果为v16.13.0,或者 v8.16.0, 要取出16或者8
  # shellcheck disable=SC2006
  node_version=`node -v | awk -F '.' '{print $1}' | sed 's/v//g'`
  if [ "$node_version" -lt 16 ]; then
    echo "node version is less than 16.x.x, please upgrade node first."
    exit 1
  fi
}

#部署目录,备份文件,以及传输文件
backupProject() {
  # shellcheck disable=SC2164
  cd ${DEPLOY_HOME}
  # 在当前目录,创建临时部署目录
  mkdir -p ${DEPLOY_HOME}/backup
  #时间戳
  # shellcheck disable=SC2006
  timestamp=`date +%Y%m%d%H%M%S`
  # 备份文件名
  # shellcheck disable=SC2006
  backup_file_name="app-${timestamp}.zip"

  # 将部署目录的app文件夹,zip压缩到临时部署目录,使用备份文件名
  # 检查${DEPLOY_HOME}/app是否存在,如果存在则压缩,否则不压缩
  if [ -d "${DEPLOY_HOME}/app" ]; then
    zip -r ${DEPLOY_HOME}/backup/${backup_file_name} ${DEPLOY_HOME}/app
  fi

  #删除部署目录的app文件夹
  rm -rf ${DEPLOY_HOME}/app
}

#启动服务
startService() {
  # shellcheck disable=SC2164
  cd ${DEPLOY_HOME}/app

  # 启动服务
  # pnpm run prod

  # pm2启动
  pm2 stop chatgpt-web-midjourney-proxy

  pm2 start node ./build/index.mjs --name chatgpt-web-midjourney-proxy
}



#build front
buildFront(){
  echo "build front"
  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}

  # 安装依赖
  pnpm install
  # 打包
  pnpm run build
}

#
buildBackend(){
  echo "build backend"
  # 进入service
  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}/service
  # 安装依赖
  pnpm install

  pnpm build
}

#清理文件
cleanFile() {
  echo "清理文件"
  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}
  rm -rf ${PROJECT_HOME}/dist
  rm -rf ${PROJECT_HOME}/service/build
  rm -rf ${PROJECT_HOME}/app
}

#启动服务方法 node 启动的方式
startProject() {
  #build front
  buildFront
  # build backend-end
  buildBackend


  # 回到项目根目录
  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}

  # # 在当前目录,创建临时部署目录
  mkdir -p ${PROJECT_HOME}/app
  # #
  cp -rf  ${PROJECT_HOME}/service/package.json ${PROJECT_HOME}/app/
  cp -rf  ${PROJECT_HOME}/service/pnpm-lock.yaml ${PROJECT_HOME}/app/

  # # shellcheck disable=SC2164
  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}/app

  pnpm install --production

  # shellcheck disable=SC2164
  cd ${PROJECT_HOME}

  # # 将前端打包后的文件,复制到临时部署目录(要包括隐藏文件 )
  cp -rf  ${PROJECT_HOME}/service/. ${PROJECT_HOME}/app/
  cp -rf  ${PROJECT_HOME}/dist ${PROJECT_HOME}/app/public


  # #备份文件
  backupProject

  # # 把当前目录的app文件夹,复制到部署目录
  cp -rf  ${PROJECT_HOME}/app/. ${DEPLOY_HOME}/app/

  #启动服务
   startService
}



case $1 in
    start)
        echo "Starting project ... "

        checkNode

        #判断程序是否已经在跑 ,如果已经在跑则不再启动 ,否则启动,在跑的标志是端口3002是否被占用
        netstat -an | grep 3002 | grep LISTEN
        if [ $? -eq 0 ]; then
            echo "The project is already running."
            exit 0
        fi
        #清理文件
        cleanFile

        #启动服务
        startProject

        #清理文件
        cleanFile

        echo "done."
        ;;

    stop)
        echo "Stopping project gate ... "
        pm2 stop chatgpt-web-midjourney-proxy
        # 端口3002的进程杀掉, 用于停止服务. mac os下不能用netstat -nlp
        kill -9 $(netstat -nlp | grep :3002 | awk '{print $7}' | awk -F"/" '{ print $1 }')
        echo "done."
        ;;

    restart)
        echo "Restarting project gate ... "
        $0 stop
        sleep 3s
        $0 start
        ;;

    *)
        echo ""
        echo ""
        echo "#######################################"
        echo ""
        echo "please input: $0 {start|stop|restart}  "
        echo ""
        echo "#######################################"
        echo ""
        echo ""

        ;;
esac
exit 0


log "Processing finished!"

把脚本保存文件名deploy_gpt.sh, 放在项目文件夹。

自己修改自己想要的 部署目录,DEPLOY_HOME变量,然后运行脚本

@Dooy
Copy link
Owner

Dooy commented May 31, 2024

帮你置顶 需要的自取

@Dooy Dooy pinned this issue May 31, 2024
@Dooy Dooy closed this as completed May 31, 2024
@24001
Copy link

24001 commented Jun 4, 2024

这个Node.js版本18.几才可以吗

@Dooy
Copy link
Owner

Dooy commented Jun 4, 2024

node.js 要求是 大于18.0

@24001
Copy link

24001 commented Jun 4, 2024

node.js 要求是 大于18.0

大哥 昨天打赏你了,你有联系方式吗,我的是suolong024那个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants