-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathaction.yaml
More file actions
90 lines (85 loc) · 4.74 KB
/
action.yaml
File metadata and controls
90 lines (85 loc) · 4.74 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
name: Cache Go Dependencies
description: Cache Go Dependencies
inputs:
save:
description: Whether this job saves caches on pushes to the default branch.
required: false
default: 'true'
key-suffix:
description: Extra suffix for cache keys (use to distinguish matrix entries that share github.job).
required: false
default: ''
runs:
using: composite
steps:
- name: Determine Go cache paths
id: cache-paths
run: |
echo "GOCACHE=$(go env GOCACHE)" >> "$GITHUB_OUTPUT"
echo "GOARCH=$(go env GOARCH)" >> "$GITHUB_OUTPUT"
echo "TAG=${{ github.sha }}" >> "$GITHUB_OUTPUT"
suffix="${{ inputs.key-suffix }}"
echo "KEY_SUFFIX=${suffix:+-$suffix}" >> "$GITHUB_OUTPUT" # prepends dash if set
shell: bash
- name: Cache Go Build (save)
if: inputs.save == 'true' && (github.event_name == 'push' && github.ref_name == github.event.repository.default_branch)
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # ratchet:actions/cache@v5
with:
path: ${{ steps.cache-paths.outputs.GOCACHE }}
key: go-build-v1-${{ github.job }}${{ steps.cache-paths.outputs.KEY_SUFFIX }}-${{ steps.cache-paths.outputs.GOARCH }}-${{ steps.cache-paths.outputs.TAG }}
restore-keys: go-build-v1-${{ github.job }}${{ steps.cache-paths.outputs.KEY_SUFFIX }}-${{ steps.cache-paths.outputs.GOARCH }}-
- name: Cache Go Build (restore)
if: ${{ !(inputs.save == 'true' && (github.event_name == 'push' && github.ref_name == github.event.repository.default_branch)) }}
uses: actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # ratchet:actions/cache/restore@v5
with:
path: ${{ steps.cache-paths.outputs.GOCACHE }}
key: go-build-v1-${{ github.job }}${{ steps.cache-paths.outputs.KEY_SUFFIX }}-${{ steps.cache-paths.outputs.GOARCH }}-${{ steps.cache-paths.outputs.TAG }}
restore-keys: go-build-v1-${{ github.job }}${{ steps.cache-paths.outputs.KEY_SUFFIX }}-${{ steps.cache-paths.outputs.GOARCH }}-
- name: Mark GOCACHE entries for stale detection
run: |
gocache="${{ steps.cache-paths.outputs.GOCACHE }}"
if [[ -d "$gocache" ]]; then
# Backdate all cache entries to year 2000. During the build/test,
# Go's markUsed() updates mtimes of accessed entries to "now"
# (it always updates when mtime is >1 hour old). The post-step
# below trims entries still at year 2000 before cache save.
timeout 120 find "$gocache" -type f -exec touch -t 200001010000 {} + || true
# Protect trim.txt: if backdated, Go's built-in Trim() sees
# "last trim was in year 2000" and deletes ALL backdated entries
# before the build starts. Setting it to now prevents this.
echo "$(date +%s)" > "$gocache/trim.txt"
echo "Marked GOCACHE entries for stale detection"
fi
shell: bash
# Registered AFTER cache steps so the post-step runs BEFORE cache save
# (GHA runs post-steps in reverse registration order).
- name: Trim stale GOCACHE entries (post-step)
uses: gacts/run-and-post-run@81b6ce503cde93862cec047c54652e45c5dca991 # v1.4.0
with:
post: >
set +e;
gocache="$(go env GOCACHE)";
[[ -d "$gocache" ]] && [[ -f "$gocache/trim.txt" ]] || { echo "Skipping GOCACHE trim"; exit 0; };
before=$(du -sm "$gocache" 2>/dev/null | cut -f1);
cutoff=$(mktemp); touch -t 200001020000 "$cutoff";
find "$gocache" -type f ! -newer "$cutoff" -delete 2>/dev/null || true;
rm -f "$cutoff";
find "$gocache" -mindepth 1 -type d -empty -delete 2>/dev/null || true;
after=$(du -sm "$gocache" 2>/dev/null | cut -f1);
echo "GOCACHE trimmed: ${before:-0}MB -> ${after:-0}MB (removed $((${before:-0} - ${after:-0}))MB)"
- name: Stabilize file mtimes for Go test cache
run: |
# Go's test cache validates inputs by (path, size, mtime) — NOT content.
# git checkout sets all mtimes to "now", which differs between CI runs,
# causing every test to miss the cache. Setting a fixed mtime makes the
# test cache hit across runs when file content hasn't changed.
#
# Risk: if a non-.go testdata file changes content but not size between
# commits, the test cache could return a stale result. This is extremely
# rare and Go's own test cache has the same inherent limitation.
git ls-files | xargs touch -t 200101010000
# Also stabilize directory mtimes — tests using os.ReadDir or
# filepath.Walk check directory mtimes too. git ls-files only
# lists files, not directories.
git ls-files | sed 's|/[^/]*$||' | sort -u | xargs touch -t 200101010000
shell: bash