每次有代码更新最烦人的当然就是手动把编译、上传、重启项目、这些操作再操作以一边。最重要的原因还是我的服务器内存小,编译容易内存不够导致报错。
下面演示GIthub Actions (CI/CD)自动化部署本项目
- 项目中创建文件.github\workflows\Deployment.yml
- 复制下面代码到文件
name: Deployment on: push: branches: [ main ] paths: - 'next/**' - 'strapi/**' #手动 workflow_dispatch: jobs: check_changes: name: 检查改变JOB runs-on: ubuntu-latest outputs: next_changed: ${{ steps.changes.outputs.next || github.event_name == 'workflow_dispatch' }} strapi_changed: ${{ steps.changes.outputs.strapi || github.event_name == 'workflow_dispatch' }} steps: - name: checkout uses: actions/checkout@v4 - name: 检查改变 uses: dorny/paths-filter@v3 id: changes with: filters: | strapi: - 'strapi/**' next: - 'next/**' build_strapi: name: 编译 Strapi needs: check_changes if: needs.check_changes.outputs.strapi_changed == 'true' runs-on: ubuntu-latest steps: - name: checkout uses: actions/checkout@v4 - name: 编译 Strapi working-directory: ./strapi run: | npm ci npm run build - name: 部署 Strapi uses: easingthemes/ssh-deploy@main with: SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} ARGS: "-rlgoDzvc -i" SOURCE: "strapi/" REMOTE_HOST: ${{ secrets.SERVER_HOST }} REMOTE_USER: ${{ secrets.SERVER_USER }} TARGET: ${{ vars.SERVER_TARGET }}/strapi/ EXCLUDE: "/public/, /node_modules/" SCRIPT_AFTER: | cd ${{ vars.SERVER_TARGET }}/strapi npm i pm2 restart blog-api build_next: name: 编译 Next needs: check_changes if: needs.check_changes.outputs.next_changed == 'true' runs-on: ubuntu-latest steps: - name: checkout uses: actions/checkout@v4 - name: 编译 Next working-directory: ./next env: NEXT_PUBLIC_API_URL: https://blog-api.canjio.com run: | npm ci npm run build - name: 部署 Next uses: easingthemes/ssh-deploy@main with: SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} ARGS: "-rlgoDzvc -i" SOURCE: "next/" REMOTE_HOST: ${{ secrets.SERVER_HOST }} REMOTE_USER: ${{ secrets.SERVER_USER }} TARGET: ${{ vars.SERVER_TARGET }}/next/ EXCLUDE: "/public/, /node_modules/" SCRIPT_AFTER: | cd ${{ vars.SERVER_TARGET }}/next npm i pm2 restart blog-web
- 原服务器配置一遍pm2,需要有名为’blog-web‘&’blog-api‘的两个项目,我这用的是PM2,如果你是其他管理,运行里对应的代码即可
- 配置secrets 和 vars 和可以访问github帮助页面 需要配置的有
- SERVER_SSH_KEY 服务器私钥
- SERVER_USER 用户名
- SERVER_HOST ip地址
- 变量 SERVER_TARGET 服务器项目路径
- NEXT_PUBLIC_API_URL 公开的api链接,不然客户端组件是无法正常工作的