|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, master, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main, master, develop] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +# 取消同一分支的之前运行,节省资源 |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +env: |
| 16 | + JAVA_VERSION: "23" |
| 17 | + JAVA_DISTRIBUTION: "temurin" |
| 18 | + CI: "true" |
| 19 | + |
| 20 | +jobs: |
| 21 | + # ============================================================================ |
| 22 | + # 代码质量检查 |
| 23 | + # ============================================================================ |
| 24 | + lint: |
| 25 | + name: Lint & Compile |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v4 |
| 30 | + |
| 31 | + - name: Setup Java |
| 32 | + uses: actions/setup-java@v4 |
| 33 | + with: |
| 34 | + java-version: ${{ env.JAVA_VERSION }} |
| 35 | + distribution: ${{ env.JAVA_DISTRIBUTION }} |
| 36 | + cache: maven |
| 37 | + |
| 38 | + - name: Cache Maven packages |
| 39 | + uses: actions/cache@v4 |
| 40 | + with: |
| 41 | + path: ~/.m2/repository |
| 42 | + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} |
| 43 | + restore-keys: | |
| 44 | + ${{ runner.os }}-maven- |
| 45 | +
|
| 46 | + - name: Compile project |
| 47 | + run: ./mvnw compile -DskipTests -q |
| 48 | + |
| 49 | + - name: Check code style with Checkstyle |
| 50 | + run: ./mvnw checkstyle:check -q || true |
| 51 | + continue-on-error: true |
| 52 | + |
| 53 | + # ============================================================================ |
| 54 | + # 单元测试 |
| 55 | + # ============================================================================ |
| 56 | + test: |
| 57 | + name: Unit Tests |
| 58 | + runs-on: ubuntu-latest |
| 59 | + needs: lint |
| 60 | + steps: |
| 61 | + - name: Checkout repository |
| 62 | + uses: actions/checkout@v4 |
| 63 | + |
| 64 | + - name: Setup Java |
| 65 | + uses: actions/setup-java@v4 |
| 66 | + with: |
| 67 | + java-version: ${{ env.JAVA_VERSION }} |
| 68 | + distribution: ${{ env.JAVA_DISTRIBUTION }} |
| 69 | + cache: maven |
| 70 | + |
| 71 | + - name: Cache Maven packages |
| 72 | + uses: actions/cache@v4 |
| 73 | + with: |
| 74 | + path: ~/.m2/repository |
| 75 | + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} |
| 76 | + restore-keys: | |
| 77 | + ${{ runner.os }}-maven- |
| 78 | +
|
| 79 | + - name: Run unit tests with coverage |
| 80 | + run: ./mvnw test -q |
| 81 | + continue-on-error: true |
| 82 | + |
| 83 | + - name: Upload coverage to Codecov |
| 84 | + uses: codecov/codecov-action@v4 |
| 85 | + if: always() |
| 86 | + with: |
| 87 | + token: ${{ secrets.CODECOV_TOKEN }} |
| 88 | + fail_ci_if_error: false |
| 89 | + files: target/site/jacoco/jacoco.xml |
| 90 | + flags: unittests |
| 91 | + name: codecov-umbrella |
| 92 | + |
| 93 | + # ============================================================================ |
| 94 | + # 集成测试 |
| 95 | + # ============================================================================ |
| 96 | + integration-test: |
| 97 | + name: Integration Tests |
| 98 | + runs-on: ubuntu-latest |
| 99 | + needs: lint |
| 100 | + |
| 101 | + # PostgreSQL 服务 |
| 102 | + services: |
| 103 | + postgres: |
| 104 | + image: postgres:16-alpine |
| 105 | + env: |
| 106 | + POSTGRES_USER: test |
| 107 | + POSTGRES_PASSWORD: test |
| 108 | + POSTGRES_DB: test |
| 109 | + options: >- |
| 110 | + --health-cmd pg_isready |
| 111 | + --health-interval 10s |
| 112 | + --health-timeout 5s |
| 113 | + --health-retries 5 |
| 114 | + ports: |
| 115 | + - 5432:5432 |
| 116 | + |
| 117 | + env: |
| 118 | + SPRING_PROFILES_ACTIVE: test |
| 119 | + SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/test |
| 120 | + SPRING_DATASOURCE_USERNAME: test |
| 121 | + SPRING_DATASOURCE_PASSWORD: test |
| 122 | + JWT_SECRET: test-secret-key-for-ci-must-be-at-least-32-chars |
| 123 | + REFRESH_TOKEN_SECRET: test-refresh-secret-key-for-ci-32chars |
| 124 | + |
| 125 | + steps: |
| 126 | + - name: Checkout repository |
| 127 | + uses: actions/checkout@v4 |
| 128 | + |
| 129 | + - name: Setup Java |
| 130 | + uses: actions/setup-java@v4 |
| 131 | + with: |
| 132 | + java-version: ${{ env.JAVA_VERSION }} |
| 133 | + distribution: ${{ env.JAVA_DISTRIBUTION }} |
| 134 | + cache: maven |
| 135 | + |
| 136 | + - name: Cache Maven packages |
| 137 | + uses: actions/cache@v4 |
| 138 | + with: |
| 139 | + path: ~/.m2/repository |
| 140 | + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} |
| 141 | + restore-keys: | |
| 142 | + ${{ runner.os }}-maven- |
| 143 | +
|
| 144 | + - name: Run integration tests |
| 145 | + run: ./mvnw verify -DskipTests=false -q |
| 146 | + continue-on-error: true |
| 147 | + |
| 148 | + # ============================================================================ |
| 149 | + # 构建检查 |
| 150 | + # ============================================================================ |
| 151 | + build: |
| 152 | + name: Build |
| 153 | + runs-on: ubuntu-latest |
| 154 | + needs: lint |
| 155 | + steps: |
| 156 | + - name: Checkout repository |
| 157 | + uses: actions/checkout@v4 |
| 158 | + |
| 159 | + - name: Setup Java |
| 160 | + uses: actions/setup-java@v4 |
| 161 | + with: |
| 162 | + java-version: ${{ env.JAVA_VERSION }} |
| 163 | + distribution: ${{ env.JAVA_DISTRIBUTION }} |
| 164 | + cache: maven |
| 165 | + |
| 166 | + - name: Cache Maven packages |
| 167 | + uses: actions/cache@v4 |
| 168 | + with: |
| 169 | + path: ~/.m2/repository |
| 170 | + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} |
| 171 | + restore-keys: | |
| 172 | + ${{ runner.os }}-maven- |
| 173 | +
|
| 174 | + - name: Build application |
| 175 | + run: ./mvnw package -DskipTests -q |
| 176 | + |
| 177 | + - name: Upload build artifacts |
| 178 | + uses: actions/upload-artifact@v4 |
| 179 | + with: |
| 180 | + name: build-output |
| 181 | + path: | |
| 182 | + target/*.jar |
| 183 | + retention-days: 7 |
| 184 | + |
| 185 | + # ============================================================================ |
| 186 | + # 依赖安全审计 |
| 187 | + # ============================================================================ |
| 188 | + security: |
| 189 | + name: Security Audit |
| 190 | + runs-on: ubuntu-latest |
| 191 | + steps: |
| 192 | + - name: Checkout repository |
| 193 | + uses: actions/checkout@v4 |
| 194 | + |
| 195 | + - name: Setup Java |
| 196 | + uses: actions/setup-java@v4 |
| 197 | + with: |
| 198 | + java-version: ${{ env.JAVA_VERSION }} |
| 199 | + distribution: ${{ env.JAVA_DISTRIBUTION }} |
| 200 | + cache: maven |
| 201 | + |
| 202 | + - name: Run OWASP Dependency Check |
| 203 | + run: ./mvnw org.owasp:dependency-check-maven:check -q || true |
| 204 | + continue-on-error: true |
| 205 | + |
| 206 | + # ============================================================================ |
| 207 | + # 依赖更新检查(仅 PR) |
| 208 | + # ============================================================================ |
| 209 | + dependency-review: |
| 210 | + name: Dependency Review |
| 211 | + runs-on: ubuntu-latest |
| 212 | + if: github.event_name == 'pull_request' |
| 213 | + steps: |
| 214 | + - name: Checkout repository |
| 215 | + uses: actions/checkout@v4 |
| 216 | + |
| 217 | + - name: Dependency Review |
| 218 | + uses: actions/dependency-review-action@v4 |
| 219 | + with: |
| 220 | + fail-on-severity: high |
| 221 | + allow-licenses: MIT, Apache-2.0, BSD-2-Clause, BSD-3-Clause, EPL-1.0, EPL-2.0, LGPL-2.1, LGPL-3.0 |
0 commit comments