Deploy #50
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 | |
| on: | |
| # 恢复push触发器,但添加过滤条件 | |
| push: | |
| branches: [ master ] # 触发分支,根据你的分支名称调整 | |
| # 允许您从 "Actions" 选项卡手动运行此工作流 | |
| workflow_dispatch: | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| # 只有当提交消息包含[deploy]标记时才执行部署 | |
| if: contains(github.event.head_commit.message, '[deploy]') | |
| steps: | |
| - name: 检验代码 | |
| uses: actions/checkout@v4 | |
| - name: 使用 Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 # 使用你项目所需的 Node.js 版本 | |
| - name: 使用官方 Action | |
| uses: pnpm/action-setup@v2 | |
| with: | |
| version: latest | |
| run_install: false | |
| - name: 启用 pnpm 缓存 | |
| uses: actions/cache@v3 | |
| with: | |
| path: ~/.pnpm-store | |
| key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm- | |
| - name: 检查 pnpm 版本 | |
| run: pnpm --version | |
| - name: 安装依赖关系 | |
| run: pnpm install | |
| - name: 构建 Vue 项目 | |
| run: pnpm run build # 使用 pnpm 构建项目 | |
| - name: 通过 SSH 部署到服务器 | |
| env: | |
| SERVER_IP: ${{ secrets.SERVER_IP }} | |
| SERVER_USER: ${{ secrets.SERVER_USER }} | |
| DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| run: | | |
| # 配置 SSH 密钥 | |
| mkdir -p ~/.ssh | |
| echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| echo -e "Host *\n\tStrictHostKeyChecking no" > ~/.ssh/config | |
| # 上传构建文件到服务器(使用 scp,可替换为 rsync) | |
| scp -r ./dist/* "$SERVER_USER"@"$SERVER_IP":"$DEPLOY_PATH" |