From fa3ff0487c32c84fa5464ce7fab25c890bbe776e Mon Sep 17 00:00:00 2001 From: lipengfei <15566300566@163.com> Date: Tue, 21 Jan 2025 11:25:53 +0800 Subject: [PATCH] Create deploy-stage.yml --- .github/workflows/deploy-stage.yml | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .github/workflows/deploy-stage.yml diff --git a/.github/workflows/deploy-stage.yml b/.github/workflows/deploy-stage.yml new file mode 100644 index 0000000..e98f734 --- /dev/null +++ b/.github/workflows/deploy-stage.yml @@ -0,0 +1,67 @@ +name: Stage Deployment + +on: + push: + branches: + - master + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + # Step 1: 检出代码 + - name: Checkout code + uses: actions/checkout@v3 + + # Step 2: 安装 Node.js 和 pnpm 环境 + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + + - name: Install pnpm + run: npm install -g pnpm + + # Step 3: 构建前端代码 + - name: Build frontend + working-directory: ./osgraph-web + run: | + pnpm install --no-frozen-lockfile + pnpm run build + + # Step 4: 安装 sshpass + - name: Install sshpass + run: sudo apt-get update && sudo apt-get install -y sshpass + + # Step 5: 上传前端构建产物到服务器 + - name: Deploy frontend + env: + DEPLOY_SERVER: ${{ secrets.DEPLOY_SERVER }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + DEPLOY_PASSWORD: ${{ secrets.DEPLOY_PASSWORD }} + DEPLOY_PATH_FRONTEND: ${{ secrets.DEPLOY_PATH_FRONTEND }} + run: | + timeout 300 sshpass -p "$DEPLOY_PASSWORD" scp -o StrictHostKeyChecking=no -r ./osgraph-web/dist/* $DEPLOY_USER@$DEPLOY_SERVER:$DEPLOY_PATH_FRONTEND + + # Step 6: 更新后端代码并重启服务 + - name: Deploy backend + env: + DEPLOY_SERVER: ${{ secrets.DEPLOY_SERVER }} + DEPLOY_USER: ${{ secrets.DEPLOY_USER }} + DEPLOY_PASSWORD: ${{ secrets.DEPLOY_PASSWORD }} + DEPLOY_PATH_BACKEND: ${{ secrets.DEPLOY_PATH_BACKEND }} + run: | + timeout 300 sshpass -p "$DEPLOY_PASSWORD" scp -o StrictHostKeyChecking=no -r ./osgraph-service/* $DEPLOY_USER@$DEPLOY_SERVER:$DEPLOY_PATH_BACKEND + sshpass -p "$DEPLOY_PASSWORD" ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_SERVER << EOF + set -e + source ~/.bashrc + source /root/osgraph/myenv/bin/activate + cd $DEPLOY_PATH_BACKEND + if [ -f ./poetry.lock ]; then + rm ./poetry.lock + fi + poetry install + lsof -ti:8000 | xargs kill -9 || true + nohup poetry run gunicorn -w 4 -b 0.0.0.0:8000 server:app > gunicorn.log 2>&1 & + EOF