|
| 1 | +name: CI · 12 门硬关 |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + |
| 8 | +# 取消旧 run,避免重复跑 |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + # 通用:checkout + 缓存 |
| 15 | + prepare: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: { fetch-depth: 0 } # 需要 diff |
| 20 | + - uses: actions/setup-python@v5 |
| 21 | + with: { python-version: '3.10', cache: pip } |
| 22 | + - run: pip install -r requirements.txt -r requirements-dev.txt |
| 23 | + - uses: actions/cache@v4 |
| 24 | + with: |
| 25 | + path: .pytest_cache |
| 26 | + key: pytest-${{ github.sha }} |
| 27 | + - uses: actions/cache@v4 |
| 28 | + with: |
| 29 | + path: ~/.cache/pip |
| 30 | + key: pip-${{ hashFiles('requirements*.txt') }} |
| 31 | + |
| 32 | + test: |
| 33 | + needs: prepare |
| 34 | + runs-on: ubuntu-latest |
| 35 | + steps: |
| 36 | + - uses: actions/checkout@v4 |
| 37 | + - uses: actions/setup-python@v5 |
| 38 | + with: { python-version: '3.10', cache: pip } |
| 39 | + - run: pip install -r requirements.txt -r requirements-dev.txt |
| 40 | + - name: 单元 + 集成 |
| 41 | + run: pytest -q |
| 42 | + - name: 覆盖率门槛 |
| 43 | + run: pytest --cov=app --cov-fail-under=80 |
| 44 | + |
| 45 | + contract: |
| 46 | + needs: prepare |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - uses: actions/checkout@v4 |
| 50 | + with: { fetch-depth: 0 } |
| 51 | + - uses: actions/setup-python@v5 |
| 52 | + with: { python-version: '3.10' } |
| 53 | + - run: pip install oasdiff spectral-cli |
| 54 | + - name: 契约 diff |
| 55 | + run: oasdiff diff origin/main HEAD --format github |
| 56 | + - name: 契约 lint |
| 57 | + run: spectral lint openapi.yaml --ruleset .spectral.yaml |
| 58 | + |
| 59 | + lint: |
| 60 | + needs: prepare |
| 61 | + runs-on: ubuntu-latest |
| 62 | + steps: |
| 63 | + - uses: actions/checkout@v4 |
| 64 | + - uses: actions/setup-python@v5 |
| 65 | + with: { python-version: '3.10' } |
| 66 | + - run: pip install ruff bandit |
| 67 | + - name: ruff |
| 68 | + run: ruff check . |
| 69 | + - name: mypy |
| 70 | + run: mypy app/ |
| 71 | + |
| 72 | + security: |
| 73 | + needs: prepare |
| 74 | + runs-on: ubuntu-latest |
| 75 | + steps: |
| 76 | + - uses: actions/checkout@v4 |
| 77 | + with: { fetch-depth: 0 } |
| 78 | + - uses: actions/setup-python@v5 |
| 79 | + with: { python-version: '3.10' } |
| 80 | + - run: pip install bandit safety |
| 81 | + - name: SAST (bandit) |
| 82 | + run: bandit -r app/ -ll |
| 83 | + - name: SCA (safety) |
| 84 | + run: safety check |
| 85 | + - name: Secrets (gitleaks) |
| 86 | + uses: gitleaks/gitleaks-action@v2 |
| 87 | + env: |
| 88 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + |
| 90 | + build: |
| 91 | + needs: [test, contract, lint, security] |
| 92 | + runs-on: ubuntu-latest |
| 93 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 94 | + steps: |
| 95 | + - uses: actions/checkout@v4 |
| 96 | + - name: 构建镜像 |
| 97 | + run: docker build -t app:${{ github.sha }} . |
| 98 | + - name: 镜像扫描 |
| 99 | + uses: aquasecurity/trivy-action@master |
| 100 | + with: |
| 101 | + image-ref: app:${{ github.sha }} |
| 102 | + severity: HIGH,CRITICAL |
| 103 | + exit-code: '1' |
| 104 | + ignore-unfixed: true |
0 commit comments