-
-
Notifications
You must be signed in to change notification settings - Fork 0
308 lines (284 loc) · 11 KB
/
Copy pathrust-ci-reusable.yml
File metadata and controls
308 lines (284 loc) · 11 KB
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
# SPDX-License-Identifier: MPL-2.0
# rust-ci-reusable.yml — Reusable Rust CI bundle (RSR).
#
# Replaces the per-repo `rust-ci.yml` template that copy-drifted across
# the estate. Estate audit (2026-05-26) found:
#
# * 137 repos shipping their own copy of rust-ci.yml
# * 30 unique SHAs — same logical workflow, drifted independently
# * Recurring failure modes across PRs: missing top-level
# `permissions:`, inconsistent `if: hashFiles('Cargo.toml')`
# guards, `cargo audit` re-installing every run, license-header
# drift (PMPL/MPL/AGPL), inconsistent SHA pins.
#
# The reusable bundles the union of features observed across the
# variants and gates the slow extras (audit, coverage) behind opt-in
# inputs so consumers only pay for what they want.
#
# Caller example (single wrapper, mirrors governance.yml + deno-ci.yml):
#
# jobs:
# rust-ci:
# uses: hyperpolymath/standards/.github/workflows/rust-ci-reusable.yml@d135b05bfc647d0c0fbfedc7e80f37ea50f49236
#
# With audit + coverage enabled:
#
# jobs:
# rust-ci:
# uses: hyperpolymath/standards/.github/workflows/rust-ci-reusable.yml@d135b05bfc647d0c0fbfedc7e80f37ea50f49236
# with:
# enable_audit: true
# enable_coverage: true
#
# Sub-crate / monorepo workspace (Cargo.toml lives in a subdirectory):
#
# jobs:
# rust-ci-cli:
# uses: hyperpolymath/standards/.github/workflows/rust-ci-reusable.yml@d135b05bfc647d0c0fbfedc7e80f37ea50f49236
# with:
# working_directory: crates/cli
# rust-ci-server:
# uses: hyperpolymath/standards/.github/workflows/rust-ci-reusable.yml@d135b05bfc647d0c0fbfedc7e80f37ea50f49236
# with:
# working_directory: crates/server
#
# Out-of-scope (left bespoke per-repo): multi-OS matrices, cross-compile
# (`cross build`), multi-Rust-version matrices. Each has too much per-repo
# variance to share a single abstraction cleanly (verified against the 5
# matrix-using repos as of 2026-05-26: julia-the-viper / verisimdb /
# verisimiser / reasonably-good-token-vault use four different matrix
# dimensions).
name: Rust CI (reusable)
on:
workflow_call:
inputs:
runs-on:
description: Runner label for all Rust CI jobs
type: string
required: false
default: ubuntu-latest
enable_audit:
description: Run `cargo audit` (slow — installs each run; off by default)
type: boolean
required: false
default: false
enable_coverage:
description: Measure line coverage with cargo-llvm-cov and enforce `coverage_floor` (off by default)
type: boolean
required: false
default: false
coverage_floor:
description: Minimum line-coverage percent when enable_coverage is set; ratchet upward, never lower.
type: string
required: false
default: "0"
clippy_args:
description: Args appended to `cargo clippy`
type: string
required: false
default: "--all-targets -- -D warnings"
test_args:
description: Args appended to `cargo test`
type: string
required: false
default: "--all-targets"
check_args:
description: Args appended to `cargo check`
type: string
required: false
default: "--all-targets"
working_directory:
description: |
Directory containing `Cargo.toml` (relative to the repo root). All
cargo invocations cd into this directory; `hashFiles()` guards also
consult it. Default `.` keeps single-crate repos unchanged. Set
to e.g. `crates/server` for a sub-crate, or pass a different
value per wrapper call when running the reusable in a workspace
via separate jobs.
type: string
required: false
default: "."
permissions:
contents: read
jobs:
# Skip the whole reusable when the repo has no Cargo.toml — lets consumers
# add the wrapper unconditionally without worrying about repos that don't
# ship Rust code yet. This MUST be a checked-out step, NOT a job-level
# `if: hashFiles(...)`: job `if:` is evaluated server-side before any
# checkout, so hashFiles() sees an empty workspace and always returns ''
# — which silently skipped every job on EVERY repo, Rust or not. The
# detect job checks out and exposes a real boolean the others gate on.
detect:
timeout-minutes: 5
name: Detect Cargo.toml
runs-on: ${{ inputs.runs-on }}
permissions:
contents: read
outputs:
has_cargo: ${{ steps.detect.outputs.has_cargo }}
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
- name: Detect Cargo.toml
id: detect
run: |
if [ -f "${{ inputs.working_directory }}/Cargo.toml" ]; then
echo "has_cargo=true" >> "$GITHUB_OUTPUT"
else
echo "has_cargo=false" >> "$GITHUB_OUTPUT"
fi
check:
timeout-minutes: 20
name: Cargo check + clippy + fmt
runs-on: ${{ inputs.runs-on }}
needs: detect
if: ${{ needs.detect.outputs.has_cargo == 'true' }}
permissions:
contents: read
defaults:
run:
working-directory: ${{ inputs.working_directory }}
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
- name: Install Rust toolchain
# `toolchain:` is mandatory because the action is pinned by commit SHA:
# dtolnay/rust-toolchain infers the toolchain from the `@`-ref (e.g.
# `@stable`), but a SHA ref carries no version, so the action's "parse
# toolchain version" step fails with `'toolchain' is a required input`.
# See standards estate-wide rust-ci red.
uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # stable
with:
toolchain: stable
components: clippy, rustfmt
- name: Cache cargo registry and build
uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2
with:
workspaces: ${{ inputs.working_directory }}
- name: Cargo check
# `--locked` so CI honours `Cargo.lock` and surfaces dep drift the
# first push it happens, instead of silently re-resolving (which
# masked the echidna#92 / echidna PR #128 dependabot major-bump
# break for 24h). See standards#295.
run: cargo check --locked ${{ inputs.check_args }}
- name: Cargo fmt
run: cargo fmt --all -- --check
- name: Cargo clippy
run: cargo clippy --locked ${{ inputs.clippy_args }}
test:
timeout-minutes: 20
name: Cargo test
runs-on: ${{ inputs.runs-on }}
needs: [detect, check]
if: ${{ needs.detect.outputs.has_cargo == 'true' }}
permissions:
contents: read
defaults:
run:
working-directory: ${{ inputs.working_directory }}
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
- name: Install Rust toolchain
# `toolchain:` mandatory under SHA pin — see Cargo check job above.
uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # stable
with:
toolchain: stable
- name: Cache cargo registry and build
uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2
with:
workspaces: ${{ inputs.working_directory }}
- name: Run tests
# `--locked` — see Cargo check above.
run: cargo test --locked ${{ inputs.test_args }}
- name: Write summary
if: always()
run: |
{
echo "## Rust CI Results"
echo ""
echo "- **cargo check**: ${{ needs.check.result }}"
echo "- **cargo test**: completed"
} >> "$GITHUB_STEP_SUMMARY"
audit:
timeout-minutes: 20
name: Cargo audit (security)
runs-on: ${{ inputs.runs-on }}
needs: detect
if: ${{ inputs.enable_audit && needs.detect.outputs.has_cargo == 'true' }}
permissions:
contents: read
defaults:
run:
working-directory: ${{ inputs.working_directory }}
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
- name: Install Rust toolchain
# `toolchain:` mandatory under SHA pin — see Cargo check job above.
uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # stable
with:
toolchain: stable
- name: Install cargo-audit
# Use the binstall path when available to skip a from-source rebuild
# on every run — was the single biggest contributor to slow CI on
# repos that opted in to audit (~3–4 minute install).
run: cargo install cargo-audit --locked
- name: Security audit
run: cargo audit
coverage:
timeout-minutes: 25
name: llvm-cov line coverage
runs-on: ${{ inputs.runs-on }}
needs: detect
if: ${{ inputs.enable_coverage && needs.detect.outputs.has_cargo == 'true' }}
permissions:
contents: read
defaults:
run:
working-directory: ${{ inputs.working_directory }}
env:
# Ratcheted line-coverage floor (from the caller). Raise toward target; never lower.
FLOOR: ${{ inputs.coverage_floor }}
steps:
- name: Checkout repository
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ github.repository }}
ref: ${{ github.ref }}
- name: Install Rust toolchain
# `toolchain:` mandatory under SHA pin — see Cargo check job above.
uses: dtolnay/rust-toolchain@2c7215f132e9ebf062739d9130488b56d53c060c # stable
with:
toolchain: stable
components: llvm-tools-preview
- name: Install cargo-llvm-cov
run: cargo install cargo-llvm-cov --locked
- name: Measure line coverage
id: cov
# Self-contained: no external coverage service. Own the gate on our runner.
run: |
set -euo pipefail
cargo llvm-cov --workspace --locked --json --output-path cov.json
PCT=$(jq -r '.data[0].totals.lines.percent' cov.json)
printf '### Line coverage: %.2f%% (floor %s%%)\n' "$PCT" "$FLOOR" >> "$GITHUB_STEP_SUMMARY"
echo "pct=$PCT" >> "$GITHUB_OUTPUT"
- name: Enforce ratchet floor
run: |
set -euo pipefail
awk -v p="${{ steps.cov.outputs.pct }}" -v f="$FLOOR" 'BEGIN {
if (p + 0 < f + 0) { printf "FAIL: line coverage %.2f%% is below floor %s%%\n", p, f; exit 1 }
printf "OK: line coverage %.2f%% >= floor %s%%\n", p, f
}'