garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# Copyright (c) 2026 Kirky.X🌠
# SPDX-License-Identifier: Apache-2.0

name: CI

on:
  push:
    branches: [main, develop, "release/*"]
  pull_request:
    branches: [main, develop]
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  RUSTFLAGS: "-D warnings"

jobs:
  # 基础检查:默认特性(default = backend-embedded)
  # sdforge build script 需要 protoc;用 taiki-e/install-action 跨平台统一安装(SHA pin 防供应链攻击)
  check-default:
    name: Check (default = backend-embedded)
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1 rustfmt,clippy
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-default-deps
          lookup-only: true
      - name: Check (default features)
        run: cargo check --locked
      - name: Build (default features)
        run: cargo build --locked

  # 全特性检查
  check-full:
    name: Check (full features)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1 rustfmt,clippy
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-full-deps
          lookup-only: true
      - name: Check (full)
        run: cargo check --features "full" --locked
      - name: Build (full)
        run: cargo build --features "full" --locked

  # 生产组合编译门禁
  # 验证 production 聚合特性与 examples workspace 全特性均可独立编译,
  # 并强制 --locked 与已提交 Cargo.lock 一致(T31)。
  check-production:
    name: Check (production + examples)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1 rustfmt,clippy
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-prod-deps
          lookup-only: true
      - name: Check (production features)
        run: cargo check --features "production" --locked
      - name: Check (examples, all features)
        run: cargo check -p garrison-examples --all-features --locked

  # 格式化检查
  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1 rustfmt
        shell: bash
      - name: Check formatting
        run: cargo fmt --all -- --check

  # Clippy
  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1 clippy
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-full-deps
          lookup-only: true
      - name: Clippy (default)
        run: cargo clippy --no-default-features --features "default" -- -D warnings
      - name: Clippy (full)
        run: cargo clippy --features "full" -- -D warnings

  # 测试矩阵
  # 工具链按腿安装:矩阵腿经 scripts/setup_rust.sh 装各自通道
  # (MSRV 腿取 Cargo.toml 的 rust-version,守卫测试断言同值)。
  # 不用 action 的原因见 scripts/setup_rust.sh 头注:dtolnay/rust-toolchain 的
  # SHA 钉位会脱离上游 ref 历史,供应链审计无法通过。
  # protoc 用 taiki-e/install-action 跨平台统一安装(Linux/macOS/Windows 均支持)
  test:
    name: Test (${{ matrix.os }} / Rust ${{ matrix.rust }})
    runs-on: ${{ matrix.os }}
    # Windows 腿需要更长时限:vcpkg 源码编译 openssl(webauthn attestation-ca
    # 依赖)+ 全特性面冷编译 ~20min,25min 上限两轮实测均到点被杀
    #(超时在 GitHub 上表现为 job cancelled)。其余平台维持 25min。
    timeout-minutes: ${{ matrix.os == 'windows-latest' && 45 || 25 }}
    env:
      # Linux runner 上 sqlite 文件库 URL 在 MSRV/beta 面触发
      # SQLITE_READONLY(预创建空文件亦然,历史 MSRV 腿实录),统一回退内存库;
      # macOS/Windows 文件库修多连接 "no such table" 偶发。
      GARRISON_TEST_SQLITE_MEMORY: ${{ matrix.os == 'ubuntu-latest' && '1' || '0' }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        rust: [1.97.1, stable, beta]
        exclude:
          - os: windows-latest
            rust: beta
          - os: macos-latest
            rust: beta
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      # 矩阵腿工具链按腿安装:1.97.1 / stable / beta 三腿各装各的。
      # 工具链值经 env 传入而非直接内插 ${{ }}:内插到 run 里会被 zizmor 判为
      # template-injection(虽然 matrix.rust 是本文件里的静态矩阵,非攻击者可控)。
      - name: Setup Rust toolchain
        env:
          RUST_TOOLCHAIN: ${{ matrix.rust }}
        run: bash scripts/setup_rust.sh "$RUST_TOOLCHAIN" rustfmt,clippy
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      # Windows runner 无系统 OpenSSL 开发文件:webauthn-rs 0.5.5 引入的
      # attestation 面经 webauthn-attestation-ca 无条件依赖 openssl,
      # full 面(protocol-webauthn)在 Windows 编译即触发 openssl-sys
      # "Could not find directory of OpenSSL installation"(2026-10 实录)。
      # 用 runner 预装 vcpkg 装 openssl(static-md 三元组,免 DLL 依赖),
      # 经 openssl-sys 的 vcpkg 发现模式(VCPKG_ROOT + VCPKGRS_TRIPLET)链接;
      # 不设 OPENSSL_DIR(choco 版 lib 布局与 openssl-sys 预期不符,已验证)。
      # Linux/macOS 自带 OpenSSL,无需此步。
      - name: "Install OpenSSL (Windows: webauthn attestation-ca)"
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          if (-not $env:VCPKG_ROOT) { $env:VCPKG_ROOT = 'C:\vcpkg' }
          & "$env:VCPKG_ROOT\vcpkg.exe" install openssl:x64-windows-static-md --disable-metrics
          if ($LASTEXITCODE -ne 0) { throw "vcpkg install openssl failed" }
          "VCPKG_ROOT=$env:VCPKG_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
          "VCPKGRS_TRIPLET=x64-windows-static-md" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-test-deps-${{ matrix.rust }}
          lookup-only: true
      - name: Test (default)
        run: cargo test --no-default-features --features "default" --lib --locked
      - name: Test (full)
        run: cargo test --features "full" --lib --locked
      - name: garrison cli-ops lib tests (ops command combo)
        # 单根二进制改造后 ops 子命令处理器与测试均在库层 src/cli/ops.rs
        run: cargo test --features "cli-ops,account-credential,protocol-zeroize,db-sqlite,protocol-jwt,config-yaml,field-encryption,dao-key-index" --lib --locked
      # 盲区补齐(2026-09-14 审计)
      # 以下 feature 面此前不在任何 job 的「运行」维度:问题潜伏数月才被发现
      # (i18n 断言漂移 39 例、keycloak OsRng E0432、macros trybuild 自 8 月损坏、
      # doctest 从未运行导致 state/impls.rs 2 例失败)。仅在 Linux/MSRV 腿执行一次。
      - name: Test (ban-sync)
        if: matrix.os == 'ubuntu-latest' && matrix.rust == '1.97.1'
        # redis 不可达时用例自行跳过,无服务依赖
        run: cargo test --no-default-features --features "ban-sync" --lib --locked
      - name: Doctests (full)
        # doctest 此前从未纳入 CI(doc job 只构建文档不做 test --doc)
        if: matrix.os == 'ubuntu-latest' && matrix.rust == '1.97.1'
        run: cargo test --features "full" --doc --locked
      - name: Check (production)
        # 生产 feature 面编译门禁(不含运行时服务依赖)
        if: matrix.os == 'ubuntu-latest' && matrix.rust == '1.97.1'
        run: cargo check --features production --locked
      - name: Test (db-sqlite)
        if: matrix.os == 'ubuntu-latest' && matrix.rust == '1.97.1'
        # 内嵌 sqlite,无服务依赖
        run: cargo test --no-default-features --features "db-sqlite" --lib --locked

  # 测试编译门禁(feature 组合下的测试代码防漂移)
  # 背景(2026-09 实录):check 不编译 #[cfg(test)] 代码,test 只覆盖 default/full
  # 两极——中间 feature 组合下的测试代码腐坏(trait 新增方法后 mock 未同步、
  # 测试引用未启用 feature 的模块、宏改名遗漏等)无法被现有 job 发现:
  # 曾因此潜伏 93 个 lib 测试编译错误 + acceptance 旧名残留 + examples DAO 缺方法。
  # 本 job 用 `--no-run` 只编译不运行,以低成本覆盖代表性中间组合
  # (与 feature-matrix.yml 的定向组合清单呼应;该工作流每周全量兜底)。
  test-compile-guard:
    name: Test compile (${{ matrix.name }})
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: default
            flags: --no-default-features --features default
          - name: minimal
            flags: --no-default-features
          - name: db-web-policy-listener
            flags: --no-default-features --features db-sqlite,web-axum,account-policy,listener
          - name: email-credential-authflow
            flags: --no-default-features --features email-verification,email-verification-smtp,account-credential,account-authflow
          - name: jwt-ct-eq
            flags: --no-default-features --features protocol-jwt,secure-ct-eq
          - name: tenant-bruteforce
            flags: --no-default-features --features tenant-isolation,firewall-bruteforce
          - name: oauth2-redis
            flags: --no-default-features --features oauth2-server,cache-redis
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      # runner 默认可用磁盘 ~14GB,workspace-under-full 的测试编译(本 job 的
      # default 组合第二步骤)会耗尽——预清理预装工具链释放 ~25GB
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \
            /usr/local/.ghcup /opt/hostedtoolcache/CodeQL /usr/lib/jvm \
            /usr/local/share/boost /usr/share/swift || true
          sudo docker image prune --all --force > /dev/null 2>&1 || true
          df -h /
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-guard-deps
          lookup-only: true
      - name: Compile tests (lib + integration, no run)
        env:
          # 不加引号:flags 含多个词(--no-default-features --features default),
          # 依赖分词展开
          TEST_FLAGS: ${{ matrix.flags }}
        run: cargo test --no-run $TEST_FLAGS --locked
      # default 组合额外做 workspace 全量验证:先清掉组合编译产物,再以
      # 无 debuginfo 模式编译(编译门禁不需调试符号,target 体积减半以上)
      - name: Clean target before workspace compile
        if: matrix.name == 'default'
        run: cargo clean
      - name: Compile workspace tests (examples/macros under full)
        if: matrix.name == 'default'
        env:
          CARGO_PROFILE_DEV_DEBUG: 0
        run: cargo test --workspace --no-run --features full --locked

  # 文档构建
  doc:
    name: Documentation
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-full-deps
          lookup-only: true
      - name: Build docs
        run: cargo doc --no-deps --features "full"
      - name: Install cargo-deadlinks
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: cargo-deadlinks
      # 死链检查(显性化策略 — 规则 12:不掩盖,分类处理)
      #
      # 根因分析(2026-07-21):死链全部来自外部 crate trait 的 blanket impl:
      #   - `Context` ← opentelemetry::FutureExt::with_context doc 中的 [`Context`] intra-doc link
      #   - `dispatcher` ← tracing_subscriber::WithSubscriber doc 中的 [default subscriber] reference link
      #   - `Response` ← warp::Reply::into_response doc 中的 [`Response`] intra-doc link
      # rustdoc 在 `--no-deps` 模式下无法解析这些外部 intra-doc link,生成相对路径 → 死链。
      # 切换 nightly + `--extern-html-root-url` 可修复,但项目用 stable Rust(版本见 Cargo.toml `rust-version`)。
      #
      # 处理策略:
      #   1. 过滤已知外部 crate 死链(Context/dispatcher/Response)— 不阻断 CI
      #   2. 新死链(garrison 自身 doc comment 问题)— 阻断 CI
      # 这样移除 continue-on-error,让死链检查成为真实门禁,同时不掩盖已知外部问题。
      - name: Check broken links
        run: |
          set -euo pipefail
          OUTPUT=$(cargo deadlinks --dir target/doc/garrison 2>&1 || true)
          echo "$OUTPUT"
          # 过滤已知外部 crate 死链(opentelemetry Context / tracing-subscriber dispatcher / warp Response)
          # 精确匹配路径片段边界(/ 或 . 分隔),避免误过滤 TenantContext/GarrisonResponse 等自身类型
          # - `config/Context does not exist` ← opentelemetry::FutureExt blanket impl
          # - `config/dispatcher does not exist` ← tracing_subscriber::WithSubscriber blanket impl
          # - `error/type.Response.html does not exist` ← warp::Reply::into_response blanket impl
          NEW_DEADLINKS=$(echo "$OUTPUT" | grep "does not exist" | grep -vE "[/.](Context|dispatcher|Response)(\.html)? does not exist" || true)
          if [ -n "$NEW_DEADLINKS" ]; then
            echo "❌ Found new deadlinks (not from external crate trait impls):"
            echo "$NEW_DEADLINKS"
            exit 1
          else
            echo "✅ No new deadlinks. Known external-crate deadlinks (Context/dispatcher/Response) filtered."
          fi

  # cargo-deny(漏洞 / 许可证 / 禁用源 / 重复依赖)
  # cargo-deny 已覆盖 cargo-audit 的漏洞检查功能,且配置统一在 deny.toml 管理。
  # 删除独立 audit job,避免维护两份忽略列表(audit.toml + deny.toml)。
  # cargo-deny 用 taiki-e/install-action 跨平台安装(与 cargo-vet / cargo-cyclonedx
  # 同一安装通道),配置统一在 deny.toml 管理。
  deny:
    name: Cargo Deny
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: cargo-deny
      - name: Generate Cargo.lock (library crate, not committed)
        run: cargo generate-lockfile
      - name: Run cargo deny
        run: cargo deny --all-features --locked check

  # 集成测试(tests/ 目录,仅 Linux + MSRV)
  integration-test:
    name: Integration Tests
    runs-on: ubuntu-latest
    env:
      GARRISON_TEST_SQLITE_MEMORY: "1"
      # R-08:真实外部服务探活使 environment.rs 门控用例(Redis DAO/Postgres 迁移)
      # 在 CI 真实执行,而非恒为 [SKIP]。
      GARRISON_TEST_REDIS: "1"
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres:
        image: postgres:16-alpine
        ports:
          - 5432:5432
        env:
          # 与 tests/acceptance/environment.rs 的 POSTGRES_URL 常量对齐(CI 专用测试凭据)
          POSTGRES_USER: garrison
          POSTGRES_PASSWORD: garrison
          POSTGRES_DB: garrison_test
        options: >-
          --health-cmd "pg_isready -U garrison"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    timeout-minutes: 25
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-full-deps
          lookup-only: true
      - name: Integration tests (full features)
        # 用 --tests 而非 --test '*':--test '*' 会显式展开所有 [[test]] target,
        # 遇到 required-features 不满足的(如 db_mysql_testcontainers 需要 db-mysql)会报错。
        # --tests 自动跳过 required-features 不满足的 test target。
        # db_mysql_testcontainers 需要 Docker + testcontainers 拉 MySQL 镜像,本地手动跑。
        # environment.rs 的 Redis/Postgres 用例经上方 services 真实执行(R-08)。
        run: cargo test --features "full" --tests --no-fail-fast

  # 覆盖率(仅 Linux)
  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    env:
      # llvm-cov 面下 sqlite 文件库 URL 触发 SQLITE_READONLY,回退内存库
      GARRISON_TEST_SQLITE_MEMORY: "1"
      # R-08:llvm-cov 会运行 lib 测试与验收测试,Redis/Postgres services 让
      # 环境门控用例(sso channel Redis pub/sub、Redis DAO、Postgres 迁移)真实执行
      GARRISON_TEST_REDIS: "1"
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres:
        image: postgres:16-alpine
        ports:
          - 5432:5432
        env:
          # 与 tests/acceptance/environment.rs 的 POSTGRES_URL 常量对齐(CI 专用测试凭据)
          POSTGRES_USER: garrison
          POSTGRES_PASSWORD: garrison
          POSTGRES_DB: garrison_test
        options: >-
          --health-cmd "pg_isready -U garrison"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Setup Rust toolchain (1.97.1)
        run: bash scripts/setup_rust.sh 1.97.1
        shell: bash
      - name: Install protoc (sdforge build dep)
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: protoc
      - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
        with:
          shared-key: ci-coverage-deps
          lookup-only: true
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@b80e2bb2635f28eb98b9fce1e2f0887e1535c6b2 # v2.90.0
        with:
          tool: cargo-llvm-cov
      - name: Run coverage (fail under 95% lines)
        run: cargo llvm-cov --features "full" --fail-under-lines 95 --lcov --output-path lcov.info
      - name: Coverage summary
        if: always()
        run: cargo llvm-cov report --summary-only
      - name: Upload coverage
        uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./lcov.info

  # 容器镜像构建与冒烟(R03 多阶段容器化交付物)
  # 验证 docker/Dockerfile(debian-slim)与 docker/Dockerfile.distroless 两条产物线:
  # 构建(cargo-chef 依赖缓存 + --locked)、compose 示例语法、容器内
  # healthcheck 探针闭环(/healthz → `garrison healthcheck` 子命令 → healthy)。
  # push:false + load:true——只构建不推送,发布镜像走 release 流程。
  docker-build:
    name: Docker build & smoke
    runs-on: ubuntu-latest
    timeout-minutes: 50
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      # 镜像构建(cargo 全量 release 依赖编译)会占用大量磁盘,与
      # test-compile-guard 同款预清理释放 ~25GB
      - name: Free disk space
        run: |
          sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc \
            /usr/local/.ghcup /opt/hostedtoolcache/CodeQL /usr/lib/jvm \
            /usr/local/share/boost /usr/share/swift || true
          sudo docker image prune --all --force > /dev/null 2>&1 || true
          df -h /
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
      - name: Build debian-slim image
        uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
        with:
          context: .
          file: ./docker/Dockerfile
          push: false
          load: true
          tags: garrison-auth:ci
          # 两变体 builder 阶段逐字相同(同 chef 基座 + 同 apt 层 + 同 cook 参数),
          # 层按 digest 去重后共享同一 cache scope 无覆盖问题;拆双 scope 只会
          # 造成同 job 内依赖树二次编译与 gha cache 双份占用(runner 层不同各存各的)
          cache-from: type=gha,scope=garrison-docker
          cache-to: type=gha,mode=max,scope=garrison-docker
          build-args: |
            VERSION=0.0.0-ci
            GIT_SHA=${{ github.sha }}
      - name: Validate compose example
        # 与本地校验同口径:API Key 缺失时 compose 的 `:?required` 应直接报错
        run: GARRISON_INTERNAL_API_KEY=ci-smoke-key.000000000000000000000000000000000000000000000000000000 docker compose -f docker/docker-compose.example.yml config -q
      - name: Smoke test (debian-slim)
        # HEALTHCHECK --interval=30s --start-period=10s 下 healthy 最早约 t≈10-40s
        # 才出现,探活成功后立即单次断言几乎必读 starting——改为有界轮询
        # Health.Status(上限 30×3s≈90s),读到 healthy 即止;超时则自然失败
        # 且最终状态可见。curl 仅作循环内兜底探测
        run: |
          set -euo pipefail
          docker run -d --name smoke-debian \
            -e GARRISON_INTERNAL_API_KEY=ci-smoke-key.000000000000000000000000000000000000000000000000000000 \
            -p 127.0.0.1:8080:8080 garrison-auth:ci
          status=""
          for _ in $(seq 1 30); do
            status="$(docker inspect -f '{{.State.Health.Status}}' smoke-debian)"
            if [ "$status" = "healthy" ]; then break; fi
            curl -sf http://127.0.0.1:8080/healthz > /dev/null 2>&1 || true
            sleep 3
          done
          echo "final Health.Status: ${status:-<inspect-failed>}"
          [ "$status" = "healthy" ]
      - name: Dump smoke-debian diagnostics
        if: failure()
        run: |
          docker logs smoke-debian || true
          docker inspect --format '{{json .State.Health}}' smoke-debian || true
      - name: Remove smoke-debian container
        if: always()
        run: docker rm -f smoke-debian || true
      - name: Build distroless image
        uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
        with:
          context: .
          file: ./docker/Dockerfile.distroless
          push: false
          load: true
          tags: garrison-auth:ci-distroless
          cache-from: type=gha,scope=garrison-docker
          cache-to: type=gha,mode=max,scope=garrison-docker
          build-args: |
            VERSION=0.0.0-ci
            GIT_SHA=${{ github.sha }}
      - name: Smoke test (distroless)
        # distroless 无 shell/curl,用镜像内单根二进制的 healthcheck 子命令做循环内兜底探测;
        # Health.Status 轮询口径与 debian 变体一致(见上)
        run: |
          set -euo pipefail
          docker run -d --name smoke-distroless \
            -e GARRISON_INTERNAL_API_KEY=ci-smoke-key.000000000000000000000000000000000000000000000000000000 garrison-auth:ci-distroless
          status=""
          for _ in $(seq 1 30); do
            status="$(docker inspect -f '{{.State.Health.Status}}' smoke-distroless)"
            if [ "$status" = "healthy" ]; then break; fi
            docker exec smoke-distroless /app/garrison healthcheck || true
            sleep 3
          done
          echo "final Health.Status: ${status:-<inspect-failed>}"
          [ "$status" = "healthy" ]
      - name: Dump smoke-distroless diagnostics
        if: failure()
        run: |
          docker logs smoke-distroless || true
          docker inspect --format '{{json .State.Health}}' smoke-distroless || true
      - name: Remove smoke-distroless container
        if: always()
        run: docker rm -f smoke-distroless || true