|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master, main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [master, main, develop] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +# 取消同一分支的之前运行,节省资源 |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +env: |
| 16 | + VITE_API_URL: /api |
| 17 | + VITE_USE_MOCK: "true" |
| 18 | + CI: "true" |
| 19 | + |
| 20 | +jobs: |
| 21 | + # ============================================================================ |
| 22 | + # 代码质量检查 |
| 23 | + # ============================================================================ |
| 24 | + lint: |
| 25 | + name: Lint & Type Check |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Setup pnpm |
| 32 | + uses: pnpm/action-setup@v4 |
| 33 | + with: |
| 34 | + version: 10.23.0 |
| 35 | + |
| 36 | + - name: Setup Node.js |
| 37 | + uses: actions/setup-node@v4 |
| 38 | + with: |
| 39 | + cache: "pnpm" |
| 40 | + |
| 41 | + - name: Get pnpm store path |
| 42 | + run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV |
| 43 | + |
| 44 | + - name: Cache pnpm store |
| 45 | + uses: actions/cache@v4 |
| 46 | + with: |
| 47 | + path: ${{ env.STORE_PATH }} |
| 48 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml') }} |
| 49 | + restore-keys: | |
| 50 | + ${{ runner.os }}-pnpm-store- |
| 51 | +
|
| 52 | + - name: Install dependencies |
| 53 | + run: pnpm install --frozen-lockfile |
| 54 | + |
| 55 | + - name: Run ESLint |
| 56 | + run: pnpm lint |
| 57 | + |
| 58 | + - name: Run TypeScript type check |
| 59 | + run: pnpm type-check |
| 60 | + |
| 61 | + # ============================================================================ |
| 62 | + # 单元测试 |
| 63 | + # ============================================================================ |
| 64 | + test: |
| 65 | + name: Unit Tests |
| 66 | + runs-on: ubuntu-latest |
| 67 | + needs: lint |
| 68 | + steps: |
| 69 | + - name: Checkout repository |
| 70 | + uses: actions/checkout@v4 |
| 71 | + |
| 72 | + - name: Setup pnpm |
| 73 | + uses: pnpm/action-setup@v4 |
| 74 | + with: |
| 75 | + version: 10.23.0 |
| 76 | + |
| 77 | + - name: Setup Node.js |
| 78 | + uses: actions/setup-node@v4 |
| 79 | + with: |
| 80 | + cache: "pnpm" |
| 81 | + |
| 82 | + - name: Get pnpm store path |
| 83 | + run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV |
| 84 | + |
| 85 | + - name: Cache pnpm store |
| 86 | + uses: actions/cache@v4 |
| 87 | + with: |
| 88 | + path: ${{ env.STORE_PATH }} |
| 89 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml') }} |
| 90 | + restore-keys: | |
| 91 | + ${{ runner.os }}-pnpm-store- |
| 92 | +
|
| 93 | + - name: Install dependencies |
| 94 | + run: pnpm install --frozen-lockfile |
| 95 | + |
| 96 | + - name: Run tests with coverage |
| 97 | + run: pnpm test:coverage |
| 98 | + continue-on-error: true |
| 99 | + |
| 100 | + - name: Upload coverage to Codecov |
| 101 | + uses: codecov/codecov-action@v4 |
| 102 | + if: always() |
| 103 | + with: |
| 104 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 105 | + fail_ci_if_error: false |
| 106 | + files: ./coverage/lcov.info |
| 107 | + flags: unittests |
| 108 | + name: codecov-umbrella |
| 109 | + |
| 110 | + # ============================================================================ |
| 111 | + # 构建检查 |
| 112 | + # ============================================================================ |
| 113 | + build: |
| 114 | + name: Build |
| 115 | + runs-on: ubuntu-latest |
| 116 | + needs: lint |
| 117 | + steps: |
| 118 | + - name: Checkout repository |
| 119 | + uses: actions/checkout@v4 |
| 120 | + |
| 121 | + - name: Setup pnpm |
| 122 | + uses: pnpm/action-setup@v4 |
| 123 | + with: |
| 124 | + version: 10.23.0 |
| 125 | + |
| 126 | + - name: Setup Node.js |
| 127 | + uses: actions/setup-node@v4 |
| 128 | + with: |
| 129 | + cache: "pnpm" |
| 130 | + |
| 131 | + - name: Get pnpm store path |
| 132 | + run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV |
| 133 | + |
| 134 | + - name: Cache pnpm store |
| 135 | + uses: actions/cache@v4 |
| 136 | + with: |
| 137 | + path: ${{ env.STORE_PATH }} |
| 138 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml') }} |
| 139 | + restore-keys: | |
| 140 | + ${{ runner.os }}-pnpm-store- |
| 141 | +
|
| 142 | + - name: Cache Vite build |
| 143 | + uses: actions/cache@v4 |
| 144 | + with: |
| 145 | + path: | |
| 146 | + node_modules/.vite |
| 147 | + key: ${{ runner.os }}-vite-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('**/*.ts', '**/*.tsx') }} |
| 148 | + restore-keys: | |
| 149 | + ${{ runner.os }}-vite-${{ hashFiles('pnpm-lock.yaml') }}- |
| 150 | +
|
| 151 | + - name: Install dependencies |
| 152 | + run: pnpm install --frozen-lockfile |
| 153 | + |
| 154 | + - name: Build application |
| 155 | + run: pnpm build |
| 156 | + |
| 157 | + - name: Upload build artifacts |
| 158 | + uses: actions/upload-artifact@v4 |
| 159 | + with: |
| 160 | + name: build-output |
| 161 | + path: dist/ |
| 162 | + retention-days: 7 |
| 163 | + |
| 164 | + # ============================================================================ |
| 165 | + # 依赖安全审计 |
| 166 | + # ============================================================================ |
| 167 | + security: |
| 168 | + name: Security Audit |
| 169 | + runs-on: ubuntu-latest |
| 170 | + steps: |
| 171 | + - name: Checkout repository |
| 172 | + uses: actions/checkout@v4 |
| 173 | + |
| 174 | + - name: Setup pnpm |
| 175 | + uses: pnpm/action-setup@v4 |
| 176 | + with: |
| 177 | + version: 10.23.0 |
| 178 | + |
| 179 | + - name: Setup Node.js |
| 180 | + uses: actions/setup-node@v4 |
| 181 | + with: |
| 182 | + cache: "pnpm" |
| 183 | + |
| 184 | + - name: Install dependencies |
| 185 | + run: pnpm install --frozen-lockfile |
| 186 | + |
| 187 | + - name: Run security audit |
| 188 | + run: pnpm audit --audit-level=high |
| 189 | + continue-on-error: true |
| 190 | + |
| 191 | + # ============================================================================ |
| 192 | + # 依赖更新检查(仅 PR) |
| 193 | + # ============================================================================ |
| 194 | + dependency-review: |
| 195 | + name: Dependency Review |
| 196 | + runs-on: ubuntu-latest |
| 197 | + if: github.event_name == 'pull_request' |
| 198 | + steps: |
| 199 | + - name: Checkout repository |
| 200 | + uses: actions/checkout@v4 |
| 201 | + |
| 202 | + - name: Dependency Review |
| 203 | + uses: actions/dependency-review-action@v4 |
| 204 | + with: |
| 205 | + fail-on-severity: high |
| 206 | + allow-licenses: MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, 0BSD |
0 commit comments