This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy NestJS to Server | |
| on: | |
| push: | |
| branches: | |
| - main # 监听 main 分支 | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '18' # 替换为你的 Node 版本 | |
| - name: Install Dependencies | |
| run: npm install --legacy-peer-deps | |
| - name: Build NestJS | |
| run: npm run build | |
| - name: Create remote directory | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.SSH_HOST }} | |
| username: ${{ secrets.SSH_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| mkdir -p ${{ secrets.REMOTE_PATH }} | |
| chmod 755 ${{ secrets.REMOTE_PATH }} | |
| - name: Upload files to server | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USERNAME }} | |
| password: ${{ secrets.SERVER_PASSWORD }} | |
| source: "dist,package.json,package-lock.json" | |
| target: ${{ secrets.REMOTE_PATH }} | |
| strip_components: 0 | |
| overwrite: true | |
| debug: true | |
| - name: SSH into server and restart with PM2 | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.SSH_HOST }} | |
| username: ${{ secrets.SSH_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| cd ${{ secrets.REMOTE_PATH }} | |
| npm install --production --legacy-peer-deps | |
| pm2 start dist/main.js --name yishe-nest || pm2 restart yishe-nest | |
| - name: Send Feishu Notification | |
| if: success() | |
| run: | | |
| curl -X POST ${{ secrets.FEISHU_WEBHOOK_URL }} \ | |
| -H "Content-Type: application/json" \ | |
| -d '{ | |
| "msg_type": "text", | |
| "content": { | |
| "text": "✅ NestJS 项目部署成功!🎉\n分支: main\n时间: '"$(date +'%Y-%m-%d %H:%M:%S')"'" | |
| } | |
| }' | |