Skip to content

chore(ci): use commit SHA for go-build cache key#19173

Merged
davdhacs merged 4 commits intomasterfrom
davdhacs/gha-cache-regular-master-updates
Mar 3, 2026
Merged

chore(ci): use commit SHA for go-build cache key#19173
davdhacs merged 4 commits intomasterfrom
davdhacs/gha-cache-regular-master-updates

Conversation

@davdhacs
Copy link
Copy Markdown
Contributor

@davdhacs davdhacs commented Feb 25, 2026

The weekly rotation (date +%Yw%U) made caches drop in usefulness within a week as code and dependencies changed. Using github.sha ensures each master push creates a fresh cache entry, improving cache hit quality for PR builds that restore from the latest master cache.

Example runs:

~6 day old cache (23 commits in master; 11538 go source lines changed; 127 changed lines in go.mod files):
https://github.com/stackrox/stackrox/actions/runs/22299402991/job/64503285263#step:5:95
2026-02-23T09:05:54Z.
21m 3s job time
Cache hit for restore-key: go-build-v1-pre-build-cli-amd64-2026w07
(cache entry saved Feb 17 19:42 UTC)

Same-day cache (1 commit in master; 34 go source lines changed; 0 changed lines in go.mod files):
https://github.com/stackrox/stackrox/actions/runs/22304627732/job/64520656630#step:5:96
2026-02-23T11:42:34Z.
5m 12s job time
Cache hit for: go-build-v1-pre-build-cli-amd64-2026w08
(cache entry saved Feb 23 09:25 UTC)

1 day old (14 commits in master; 564 go source lines changed; 0 changed lines in go.mod files):
https://github.com/stackrox/stackrox/actions/runs/22384241635/job/64791416091?pr=19174#step:5:93
2026-02-25T05:53:38Z.
5m 44s job time
Cache hit for: go-build-v1-pre-build-cli-amd64-2026w08
(cache entry saved Feb 23 09:25 UTC)

Partially generated by AI.

The weekly rotation (`date +%Yw%U`) made caches drop to 50% relevant
within a week as dependencies changed. Using `github.sha` ensures each
master push creates a fresh cache entry, improving cache hit quality for
PR builds that restore from the latest master cache.

Partially generated by AI.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@openshift-ci
Copy link
Copy Markdown

openshift-ci bot commented Feb 25, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The expression echo "TAG=${{ github.sha }}" inside the run script won’t be interpolated by GitHub Actions; instead, pass github.sha via env (or with) and reference it as "TAG=$GITHUB_SHA" in the shell to ensure the cache key is set correctly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The expression `echo "TAG=${{ github.sha }}"` inside the `run` script won’t be interpolated by GitHub Actions; instead, pass `github.sha` via `env` (or `with`) and reference it as `"TAG=$GITHUB_SHA"` in the shell to ensure the cache key is set correctly.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@rhacs-bot
Copy link
Copy Markdown
Contributor

rhacs-bot commented Feb 25, 2026

Images are ready for the commit at 688a3d5.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.11.x-232-g688a3d5db8.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.62%. Comparing base (541c90d) to head (688a3d5).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19173   +/-   ##
=======================================
  Coverage   49.62%   49.62%           
=======================================
  Files        2680     2680           
  Lines      202195   202195           
=======================================
  Hits       100333   100333           
  Misses      94386    94386           
  Partials     7476     7476           
Flag Coverage Δ
go-unit-tests 49.62% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@davdhacs
Copy link
Copy Markdown
Contributor Author

  • The expression echo "TAG=${{ github.sha }}" inside the run script won’t be interpolated by GitHub Actions; instead, pass github.sha via env (or with) and

Strange that this is suggested. It is not true as confirmed in the logs: https://github.com/stackrox/stackrox/actions/runs/22383687632/job/64789749598?pr=19173#step:5:23
echo "TAG=3ba3ac6aafd86a09662bbee94e039be9b76227eb" >> "$GITHUB_OUTPUT"

@davdhacs davdhacs marked this pull request as ready for review February 25, 2026 06:28
@davdhacs davdhacs requested a review from a team as a code owner February 25, 2026 06:28
@davdhacs davdhacs requested a review from janisz February 25, 2026 06:28
@davdhacs davdhacs requested a review from porridge February 26, 2026 05:09
Copy link
Copy Markdown
Contributor

@porridge porridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will work as intended. 🤔

First I took a look at the documentation for the restore action and it seems restore-keys needs to be a list but we are passing a string. Does this mean it is as if we never passed restore keys at all? 🤔 OTOH, in another place docs claim this needs to be a multiline string...

Now, using the SHA alone means a build of any other commit will be guaranteed to encounter an initial cache miss. So it is crucial to ensure that the restore keys provide a good fallback. But it's not clear to me how this will work... if there's no go-build-v1-whatever-amd64-deadbeefdeadbeef, then which of the hundreds of other go-build-v1-whatever-amd64- keys is going to be picked? 🤷🏻

@davdhacs
Copy link
Copy Markdown
Contributor Author

davdhacs commented Mar 2, 2026

I'm not sure this will work as intended. 🤔

First I took a look at the documentation for the restore action and it seems restore-keys needs to be a list but we are passing a string. Does this mean it is as if we never passed restore keys at all? 🤔 OTOH, in another place docs claim this needs to be a multiline string...

Good call. It looks like this issue with the documentation was raised before and partly fixed: actions/cache#1434 (comment)
It works with a single string arg or multiline string. (the cache action is typescript and splits the string by newlines here: https://github.com/actions/cache/blob/main/src/utils/actionUtils.ts#L39)
Here is an example of it working for the runs on this PR:
https://github.com/stackrox/stackrox/actions/runs/22383687632/job/64790668582?pr=19173#step:5:102

@davdhacs
Copy link
Copy Markdown
Contributor Author

davdhacs commented Mar 2, 2026

Now, using the SHA alone means a build of any other commit will be guaranteed to encounter an initial cache miss. So it is crucial to ensure that the restore keys provide a good fallback. But it's not clear to me how this will work... if there's no go-build-v1-whatever-amd64-deadbeefdeadbeef, then which of the hundreds of other go-build-v1-whatever-amd64- keys is going to be picked? 🤷🏻

TLDR: prefix match of keys sorted by last_accessed_at and namespaced to PR+master (can only load cache saved by other actions on the PR, or by master).

GHA picks up the latest used prefix match of a key (service-side. I didn't find docs about a deterministic ordering (https://docs.github.com/en/actions/reference/workflows-and-actions/dependency-caching#cache-hits-and-misses), but in usage it sorts by last accessed(saved or restored)). And the cache keys are namespaced: Runs on PRs to master can only load caches saved for that PR or from master.
So when a PR run does not match on prefix-SHA, the restore-key will find the latest cache entry saved for that job by master. By changing this to the commit-sha, the latest cache entry will be the one saved by the latest master commit instead of the first run on master in that week.

The github cli shows the best sorting doc/explanation I could find. I assume the service for the cli behaves the same as the one used in the action:

$ gh cache list -h
...
  -S, --sort string       Sort fetched caches: {created_at|last_accessed_at|size_in_bytes} (default "last_accessed_at")
...
$ gh cache list --key go-build-v1-pre-build-go-binaries-amd64-1c5c12f
No caches found in stackrox/stackrox
$ gh cache list --key go-build-v1-pre-build-go-binaries-amd64-

Showing 8 of 8 caches in stackrox/stackrox

ID          KEY                                                                           SIZE        CREATED             ACCESSED
3054929725  go-build-v1-pre-build-go-binaries-amd64-2026w09                               918.54 MiB  about 11 hours ago  about 4 minutes ago
2907799163  go-build-v1-pre-build-go-binaries-amd64-5d27bd3f661df3a8f8229f3611fd72d66...  915.71 MiB  about 11 days ago   about 26 minutes ago
2899227195  go-build-v1-pre-build-go-binaries-amd64-d1126478cc9028acfa4bdfececc52cee8...  963.33 MiB  about 12 days ago   about 6 hours ago
3058374633  go-build-v1-pre-build-go-binaries-amd64-32a6f71a40a99d79f41b82c6b72b408d3...  916.24 MiB  about 7 hours ago   about 7 hours ago
...

@davdhacs davdhacs requested a review from porridge March 2, 2026 20:00
@davdhacs davdhacs enabled auto-merge (squash) March 3, 2026 20:50
@davdhacs davdhacs merged commit fe658b3 into master Mar 3, 2026
94 checks passed
@davdhacs davdhacs deleted the davdhacs/gha-cache-regular-master-updates branch March 3, 2026 23:06
ksurabhi91 pushed a commit that referenced this pull request Mar 12, 2026
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants