|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main, develop] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +# 取消同一分支的之前运行,节省资源 |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +env: |
| 16 | + PYTHON_VERSION: "3.11" |
| 17 | + CI: "true" |
| 18 | + |
| 19 | +jobs: |
| 20 | + # ============================================================================ |
| 21 | + # 代码质量检查 |
| 22 | + # ============================================================================ |
| 23 | + lint: |
| 24 | + name: Lint & Type Check |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + |
| 30 | + - name: Setup Python |
| 31 | + uses: actions/setup-python@v5 |
| 32 | + with: |
| 33 | + python-version: ${{ env.PYTHON_VERSION }} |
| 34 | + |
| 35 | + - name: Cache pip dependencies |
| 36 | + uses: actions/cache@v4 |
| 37 | + with: |
| 38 | + path: ~/.cache/pip |
| 39 | + key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} |
| 40 | + restore-keys: | |
| 41 | + ${{ runner.os }}-pip- |
| 42 | +
|
| 43 | + - name: Install dependencies |
| 44 | + run: | |
| 45 | + python -m pip install --upgrade pip |
| 46 | + pip install -e ".[dev]" |
| 47 | +
|
| 48 | + - name: Run Ruff linter |
| 49 | + run: ruff check app tests |
| 50 | + |
| 51 | + - name: Run Black formatter check |
| 52 | + run: black --check app tests |
| 53 | + |
| 54 | + - name: Run MyPy type checker |
| 55 | + run: mypy app --ignore-missing-imports |
| 56 | + continue-on-error: true |
| 57 | + |
| 58 | + # ============================================================================ |
| 59 | + # 单元测试 |
| 60 | + # ============================================================================ |
| 61 | + test: |
| 62 | + name: Unit Tests |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: lint |
| 65 | + steps: |
| 66 | + - name: Checkout repository |
| 67 | + uses: actions/checkout@v4 |
| 68 | + |
| 69 | + - name: Setup Python |
| 70 | + uses: actions/setup-python@v5 |
| 71 | + with: |
| 72 | + python-version: ${{ env.PYTHON_VERSION }} |
| 73 | + |
| 74 | + - name: Cache pip dependencies |
| 75 | + uses: actions/cache@v4 |
| 76 | + with: |
| 77 | + path: ~/.cache/pip |
| 78 | + key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} |
| 79 | + restore-keys: | |
| 80 | + ${{ runner.os }}-pip- |
| 81 | +
|
| 82 | + - name: Install dependencies |
| 83 | + run: | |
| 84 | + python -m pip install --upgrade pip |
| 85 | + pip install -e ".[dev]" |
| 86 | +
|
| 87 | + - name: Run tests with coverage |
| 88 | + env: |
| 89 | + DATABASE_URL: sqlite:///./test.db |
| 90 | + JWT_SECRET_KEY: test-secret-key-for-ci |
| 91 | + JWT_REFRESH_SECRET_KEY: test-refresh-secret-key-for-ci |
| 92 | + ENVIRONMENT: test |
| 93 | + run: pytest --cov=app --cov-report=xml --cov-report=term-missing tests/ |
| 94 | + |
| 95 | + - name: Upload coverage to Codecov |
| 96 | + uses: codecov/codecov-action@v4 |
| 97 | + if: always() |
| 98 | + with: |
| 99 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 100 | + fail_ci_if_error: false |
| 101 | + files: ./coverage.xml |
| 102 | + flags: unittests |
| 103 | + name: codecov-umbrella |
| 104 | + |
| 105 | + # ============================================================================ |
| 106 | + # E2E 测试 |
| 107 | + # ============================================================================ |
| 108 | + e2e: |
| 109 | + name: E2E Tests |
| 110 | + runs-on: ubuntu-latest |
| 111 | + needs: lint |
| 112 | + |
| 113 | + # PostgreSQL 服务 |
| 114 | + services: |
| 115 | + postgres: |
| 116 | + image: postgres:16-alpine |
| 117 | + env: |
| 118 | + POSTGRES_USER: test |
| 119 | + POSTGRES_PASSWORD: test |
| 120 | + POSTGRES_DB: halolight_test |
| 121 | + options: >- |
| 122 | + --health-cmd pg_isready |
| 123 | + --health-interval 10s |
| 124 | + --health-timeout 5s |
| 125 | + --health-retries 5 |
| 126 | + ports: |
| 127 | + - 5432:5432 |
| 128 | + |
| 129 | + env: |
| 130 | + ENVIRONMENT: test |
| 131 | + DATABASE_URL: postgresql://test:test@localhost:5432/halolight_test |
| 132 | + JWT_SECRET_KEY: test-secret-key-for-ci |
| 133 | + JWT_REFRESH_SECRET_KEY: test-refresh-secret-key-for-ci |
| 134 | + |
| 135 | + steps: |
| 136 | + - name: Checkout repository |
| 137 | + uses: actions/checkout@v4 |
| 138 | + |
| 139 | + - name: Setup Python |
| 140 | + uses: actions/setup-python@v5 |
| 141 | + with: |
| 142 | + python-version: ${{ env.PYTHON_VERSION }} |
| 143 | + |
| 144 | + - name: Cache pip dependencies |
| 145 | + uses: actions/cache@v4 |
| 146 | + with: |
| 147 | + path: ~/.cache/pip |
| 148 | + key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} |
| 149 | + restore-keys: | |
| 150 | + ${{ runner.os }}-pip- |
| 151 | +
|
| 152 | + - name: Install dependencies |
| 153 | + run: | |
| 154 | + python -m pip install --upgrade pip |
| 155 | + pip install -e ".[dev]" |
| 156 | +
|
| 157 | + - name: Run database migrations |
| 158 | + run: alembic upgrade head |
| 159 | + |
| 160 | + - name: Run E2E tests |
| 161 | + run: | |
| 162 | + if [ -d "tests/e2e" ]; then |
| 163 | + pytest tests/e2e/ -v --tb=short |
| 164 | + else |
| 165 | + echo "E2E tests directory not found, skipping..." |
| 166 | + fi |
| 167 | +
|
| 168 | + # ============================================================================ |
| 169 | + # 构建检查 |
| 170 | + # ============================================================================ |
| 171 | + build: |
| 172 | + name: Build |
| 173 | + runs-on: ubuntu-latest |
| 174 | + needs: lint |
| 175 | + steps: |
| 176 | + - name: Checkout repository |
| 177 | + uses: actions/checkout@v4 |
| 178 | + |
| 179 | + - name: Setup Python |
| 180 | + uses: actions/setup-python@v5 |
| 181 | + with: |
| 182 | + python-version: ${{ env.PYTHON_VERSION }} |
| 183 | + |
| 184 | + - name: Cache pip dependencies |
| 185 | + uses: actions/cache@v4 |
| 186 | + with: |
| 187 | + path: ~/.cache/pip |
| 188 | + key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} |
| 189 | + restore-keys: | |
| 190 | + ${{ runner.os }}-pip- |
| 191 | +
|
| 192 | + - name: Install dependencies |
| 193 | + run: | |
| 194 | + python -m pip install --upgrade pip |
| 195 | + pip install -e ".[dev]" |
| 196 | + pip install build |
| 197 | +
|
| 198 | + - name: Build package |
| 199 | + run: python -m build |
| 200 | + |
| 201 | + - name: Upload build artifacts |
| 202 | + uses: actions/upload-artifact@v4 |
| 203 | + with: |
| 204 | + name: build-output |
| 205 | + path: | |
| 206 | + dist/ |
| 207 | + retention-days: 7 |
| 208 | + |
| 209 | + # ============================================================================ |
| 210 | + # Docker 构建检查 |
| 211 | + # ============================================================================ |
| 212 | + docker: |
| 213 | + name: Docker Build |
| 214 | + runs-on: ubuntu-latest |
| 215 | + needs: [lint, test] |
| 216 | + steps: |
| 217 | + - name: Checkout repository |
| 218 | + uses: actions/checkout@v4 |
| 219 | + |
| 220 | + - name: Set up Docker Buildx |
| 221 | + uses: docker/setup-buildx-action@v3 |
| 222 | + |
| 223 | + - name: Build Docker image |
| 224 | + uses: docker/build-push-action@v5 |
| 225 | + with: |
| 226 | + context: . |
| 227 | + push: false |
| 228 | + tags: halolight-api-python:${{ github.sha }} |
| 229 | + cache-from: type=gha |
| 230 | + cache-to: type=gha,mode=max |
| 231 | + |
| 232 | + # ============================================================================ |
| 233 | + # 依赖安全审计 |
| 234 | + # ============================================================================ |
| 235 | + security: |
| 236 | + name: Security Audit |
| 237 | + runs-on: ubuntu-latest |
| 238 | + steps: |
| 239 | + - name: Checkout repository |
| 240 | + uses: actions/checkout@v4 |
| 241 | + |
| 242 | + - name: Setup Python |
| 243 | + uses: actions/setup-python@v5 |
| 244 | + with: |
| 245 | + python-version: ${{ env.PYTHON_VERSION }} |
| 246 | + |
| 247 | + - name: Install dependencies |
| 248 | + run: | |
| 249 | + python -m pip install --upgrade pip |
| 250 | + pip install -e . |
| 251 | + pip install bandit safety pip-audit |
| 252 | +
|
| 253 | + - name: Run Bandit security linter |
| 254 | + run: bandit -r app -ll -ii |
| 255 | + continue-on-error: true |
| 256 | + |
| 257 | + - name: Run pip-audit for vulnerabilities |
| 258 | + run: pip-audit |
| 259 | + continue-on-error: true |
| 260 | + |
| 261 | + # ============================================================================ |
| 262 | + # 依赖更新检查(仅 PR) |
| 263 | + # ============================================================================ |
| 264 | + dependency-review: |
| 265 | + name: Dependency Review |
| 266 | + runs-on: ubuntu-latest |
| 267 | + if: github.event_name == 'pull_request' |
| 268 | + steps: |
| 269 | + - name: Checkout repository |
| 270 | + uses: actions/checkout@v4 |
| 271 | + |
| 272 | + - name: Dependency Review |
| 273 | + uses: actions/dependency-review-action@v4 |
| 274 | + with: |
| 275 | + fail-on-severity: high |
| 276 | + allow-licenses: MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, ISC, 0BSD, PSF-2.0 |
0 commit comments