Skip to content

Commit 22fe689

Browse files
committed
ci: regenerate from the committed spec so the drift check is hermetic
1 parent 3c45f9b commit 22fe689

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ jobs:
1818
with:
1919
go-version: stable
2020

21+
# Hermetic: regenerates from the committed spec, so this verifies that the committed
22+
# generated code still reproduces from it, with no network dependency. Refreshing the
23+
# spec itself is the release workflow's job.
2124
- name: Codegen drift check
25+
env:
26+
ROXYAPI_SPEC_FILE: specs/openapi.json
2227
run: |
2328
go run ./tools/roxygen generate
2429
git diff --exit-code -- specs/openapi.json roxyapi.gen.go roxy.gen.go \

tools/roxygen/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ func check(err error, msg string) {
7474
// ─── generate ────────────────────────────────────────────────────────────────
7575

7676
func generate() {
77-
fmt.Printf("Fetching OpenAPI spec from %s\n", specURL)
78-
raw := fetchSpec()
77+
raw := loadSpec()
7978

8079
dec := json.NewDecoder(bytes.NewReader(raw))
8180
dec.UseNumber()
@@ -114,6 +113,20 @@ func generate() {
114113
syncDocs()
115114
}
116115

116+
// loadSpec returns the raw spec bytes, from disk when ROXYAPI_SPEC_FILE is set and from the API
117+
// otherwise. Reading from a file keeps generation offline and byte-reproducible, which is what the
118+
// codegen drift check in CI relies on.
119+
func loadSpec() []byte {
120+
if path := os.Getenv("ROXYAPI_SPEC_FILE"); path != "" {
121+
fmt.Printf("Reading OpenAPI spec from %s (offline, ROXYAPI_SPEC_FILE)\n", path)
122+
raw, err := os.ReadFile(path)
123+
check(err, "read spec file")
124+
return raw
125+
}
126+
fmt.Printf("Fetching OpenAPI spec from %s\n", specURL)
127+
return fetchSpec()
128+
}
129+
117130
// fetchSpec retries with exponential backoff: a transient upstream error (e.g. a
118131
// CDN 520) must not fail the daily release run.
119132
func fetchSpec() []byte {

0 commit comments

Comments
 (0)