【前端进阶】代码自动化部署 Git Action / jenkins 的使用方法

381 阅读2分钟

场景假设

敏 捷 开 发 中,我们经常会遇到写完代码后部署项目的问题

这个流程通常是

  • 提交代码

git push

  • 登陆服务器

ssh server_host

cd /www/wwwroot/target_dir

  • 运行项目

npm install

npm run build

npm run start

这样就能跑起来一个普通的项目了

Next.js Nuxt.js Nest.js

但是,在你每一次敏 捷 开 发中,比如修改了一个变量,一个字符串

你猛然发现,每一次都tm要重新走一遍上面的流程

沙雕表情.gif

那么有什么办法能解决这个问题呢。

解决问题

第一种方法:云服务器

这里指的是 Vercel || 腾讯云 Serverless应用 等一众产品

这类产品会自动帮你写好部署脚本,监听git提交等一系列操作,适合简单的应用部署。

你可以绑定一个Github账号,在git push后会监听变化然后更新网站代码

如果用的是Next.jsSSR应用 React.js Vue.jsSPA应用

  • 可以使用

    • 直接进入两者的配置页面配置相关框架即可

如果用的是Node.jsAPI应用

  • 可以使用

    • Vercel的 serverless function

    • 腾讯云的 云函数

第二种方法:CI/CD(Github action | Gitlab action | Jenkins)

Continuous Integration (CI) / Continuous Delivery (CD)

持续集成 / 持续部署

Github action

可以在你的github的个人代码仓库中,上面一栏的action选项中配置

也可以在项目中创建.github/workflows/****.yml

github会识别.github/workflows/这个文件夹下面的文件在每次提交后去执行

进入action 网页捕获_22-4-2022_20508_github.com.jpeg 创建一个workflows 网页捕获_22-4-2022_205116_github.com.jpeg

默认生成的配置代码👇

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI
# 监听在什么分支什么操作后会执行这个文件
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

上面会在workspace中执行 安装、打包、测试

如有密钥等可以在仓库setting设置
使用方式如:
    设置了一个title为SERVER_HOST的密钥
    在yml文件中使用${{secrets.SERVER_HOST}}可以拿到这个密钥变量

随后你可以使用github action 插件把打包后的代码上传到服务器中

如 FTP、SSH 方法

github action 插件在哪里找?

建议搜索按照安装量最多的使用

网页捕获_22-4-2022_21314_github.com.jpeg

这里有几种方式去实现远程部署

第一种:FTP

思路:首先在 workspace 中打包出 dist 文件,把 dist 文件 FTP 上传到服务器中

优点

dist文件小 上传快
部署静态文件比较简单便捷

缺点

“怎么我本地部署好好的,服务器上就不行了”😂
平台安装依赖运行问题
这里可能导致部署失败的问题较广,如服务器环境问题,运行权限问题,npm包运行问题等。

第二种:SSH

思路:这种方法首先要创建一个 SSH Key 复制到 github 中避免网络问题,然后SSH连接到服务器中,通过git clone 克隆代码,build && start && pm2 开启自启动

使用 pm2 是部署 node.js API接口时,为了防止服务器重启后要重跑start命令

优点

这种方法因为是在服务器中运行所有命令,解决了环境不一致,npm权限问题等
部署速度 git clone 相比 FTP 上传更快一点

缺点

相比于 云服务器、FTP 操作上可能比较麻烦

Gitlab action / Jenkins

部署流程同理

完整代码

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        # node-version: [12.x, 14.x, 16.x]
        node-version: [16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      # - name: 📂 Sync files
      #   uses: SamKirkland/FTP-Deploy-Action@4.3.0
      #   with:
      #     server: ${{ secrets.FTP_HOST }}
      #     username: ${{ secrets.FTP_USERNAME }}
      #     password: ${{ secrets.FTP_PASSWORD }}
      #     server-dir: /record_api/
      - name: 🖥️ SSH
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: |
            cd /www/wwwroot/
            rm record_api/ -rf
            mkdir record_api && cd record_api
            git clone git@github.com:weipengzou/records_app_api.git -b master . 
            npm install
            npm run build:prod
            pm2 delete all
            pm2 --name record_api start npm -- run start:prod -i max
            pm2 startup
            pm2 save
          port: 22

总结

原理同 把大象装进冰箱 步骤相同

无论大象多大,只要 3 个步骤

打开冰箱

监听到 git 变化

装入大象

触发了脚本命令(FTP SSH)

关冰箱门

执行流程完毕

配置完你就可以在你每次 push 代码后给阿姨倒杯卡布奇诺了。

到点关机下班一天工资到手.gif

Over