Skip to content

持续集成 CI

假如有一个十个人的团队,每天开发者都会多次提交代码到仓库中,每次提交代码到仓库中, 对于每次推送到仓库,您可以创建一组脚本来自动构建和测试您的应用程序。这些脚本有助于减少您在应用程序中引入错误的机会。

这种做法称为持续集成(Continuous Integration)简称 CI。提交给应用程序的每个更改,甚至是开发分支,都会自动且连续地构建和测试。这些测试可确保更改通过您为应用程序建立的所有测试、指南和代码合规性标准。

  • 代码质量检测
  • 性能测试
  • 单元测试
  • 依赖扫描

代码质量检测

@antfu/eslint-config

安装 eslint

sh
pnpm add -D eslint @antfu/eslint-config

创建 .eslintrc, 配置 eslint 如何效验代码

json
{
  "extends": "@antfu"
}

package.json 中添加脚本命令

json
{
  "scripts": {
    "lint": "eslint .",
    "lint:fix": "eslint . --fix"
  }
}

TypeScript

sh
pnpm add typescript -D

创建并配置 tsconfig.json

json
{
  "compilerOptions": {
    "target": "es2018",
    "module": "esnext",
    "lib": ["esnext"],
    "moduleResolution": "node",
    "esModuleInterop": true,
    "strict": true,
    "strictNullChecks": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "skipDefaultLibCheck": true
  }
}

package.json 中添加脚本命令

json
{
  "scripts": {
    "typecheck": "tsc --noEmit"
  }
}

单元测试

yml
name: CI

on:
  push:
    branches:
      - main

  pull_request:
    branches:
      - main

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install pnpm
        uses: pnpm/action-setup@v2

      - name: Set node
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: pnpm

      - name: Setup
        run: npm i -g @antfu/ni

      - name: Install
        run: nci

      - name: Lint
        run: nr lint

  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install pnpm
        uses: pnpm/action-setup@v2

      - name: Set node
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: pnpm

      - name: Setup
        run: npm i -g @antfu/ni

      - name: Install
        run: nci

      - name: Typecheck
        run: nr typecheck

  test:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        node: [16.x, 18.x]
        os: [ubuntu-latest, windows-latest, macos-latest]
      fail-fast: false

    steps:
      - uses: actions/checkout@v3

      - name: Install pnpm
        uses: pnpm/action-setup@v2

      - name: Set node ${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}
          cache: pnpm

      - name: Setup
        run: npm i -g @antfu/ni

      - name: Install
        run: nci

      - name: Build
        run: nr build

      - name: Test
        run: nr test

文件大小检查

.github/workflows/size-check.yml

yaml
name: size
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  size:
    runs-on: ubuntu-latest
    env:
      CI_JOB_NUMBER: 1
    steps:
      - uses: actions/checkout@v1
      - uses: bahmutov/npm-install@v1

      - uses: posva/size-check-action@v1.1.2
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          build_script: size
          files: packages/vue/dist/vue.global.prod.js packages/runtime-dom/dist/runtime-dom.global.prod.js packages/size-check/dist/size-check.global.prod.js