Skip to content

Commit e3c81ed

Browse files
BUILD-9795 Add support for additional cache fallback branch
1 parent f4c9b6f commit e3c81ed

File tree

4 files changed

+82
-43
lines changed

4 files changed

+82
-43
lines changed

.github/workflows/test-action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jobs:
6565
go-${{ runner.os }}-${{ hashFiles('**/go.mod') }}
6666
go-${{ runner.os }}-
6767
fail-on-cache-miss: false
68+
fallback-branch: refs/heads/branch-2
6869
environment: dev
6970
- name: Check Go cache hit result
7071
run: |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ For a feature branch `feature/new-ui`, this will search for:
7171
| `path` | Files, directories, and wildcard patterns to cache | Yes | |
7272
| `key` | Explicit key for restoring and saving cache | Yes | |
7373
| `restore-keys` | Ordered list of prefix-matched keys for fallback | No | |
74+
| `fallback-branch` | Optional maintenance branch for fallback restore keys (pattern: `branch-*`). If not set, the repository default branch is used. | No | |
7475
| `environment` | Environment to use (dev or prod) | No | `prod` |
7576
| `upload-chunk-size` | Chunk size for large file uploads (bytes) | No | |
7677
| `enableCrossOsArchive` | Enable cross-OS cache compatibility | No | `false` |

action.yml

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ inputs:
2525
environment:
2626
description: Environment to use (dev or prod)
2727
default: prod
28+
fallback-branch:
29+
description: Optional maintenance branch for fallback restore keys (pattern branch-*). If not set, the repository default branch is used.
2830

2931
outputs:
3032
cache-hit:
@@ -97,49 +99,13 @@ runs:
9799
- name: Prepare cache keys
98100
shell: bash
99101
id: prepare-keys
100-
run: |
101-
# Use GITHUB_HEAD_REF for PR events, GITHUB_REF for push events
102-
BRANCH_NAME="${GITHUB_HEAD_REF:-$GITHUB_REF}"
103-
BRANCH_KEY="${BRANCH_NAME}/${{ inputs.key }}"
104-
echo "branch-key=${BRANCH_KEY}" >> $GITHUB_OUTPUT
105-
106-
# Process restore keys: keep branch-specific keys and add fallback to default branch
107-
if [ -n "${{ inputs.restore-keys }}" ]; then
108-
RESTORE_KEYS=""
109-
# First, add branch-specific restore keys
110-
while IFS= read -r line; do
111-
if [ -n "$line" ]; then
112-
if [ -n "$RESTORE_KEYS" ]; then
113-
RESTORE_KEYS="${RESTORE_KEYS}"$'\n'"${BRANCH_NAME}/${line}"
114-
else
115-
RESTORE_KEYS="${BRANCH_NAME}/${line}"
116-
fi
117-
fi
118-
done <<< "${{ inputs.restore-keys }}"
119-
120-
# Get the default branch dynamically using GitHub API
121-
DEFAULT_BRANCH=$(curl -s -H "Authorization: token ${{ github.token }}" \
122-
"https://api.github.com/repos/${{ github.repository }}" | \
123-
jq -r '.default_branch')
124-
125-
# Only allow fallback to supported default branches
126-
case "$DEFAULT_BRANCH" in
127-
main|master|branch-*)
128-
while IFS= read -r line; do
129-
if [ -n "$line" ]; then
130-
RESTORE_KEYS="${RESTORE_KEYS}"$'\n'"refs/heads/${DEFAULT_BRANCH}/${line}"
131-
fi
132-
done <<< "${{ inputs.restore-keys }}"
133-
;;
134-
*)
135-
echo "::warning::Default branch '$DEFAULT_BRANCH' is not supported for cache fallback. Supported branches: main, master, branch-*"
136-
;;
137-
esac
138-
139-
echo "branch-restore-keys<<EOF" >> $GITHUB_OUTPUT
140-
echo "$RESTORE_KEYS" >> $GITHUB_OUTPUT
141-
echo "EOF" >> $GITHUB_OUTPUT
142-
fi
102+
env:
103+
INPUT_KEY: ${{ inputs.key }}
104+
INPUT_RESTORE_KEYS: ${{ inputs.restore-keys }}
105+
INPUT_FALLBACK_BRANCH: ${{ inputs.fallback-branch }}
106+
GITHUB_TOKEN: ${{ github.token }}
107+
GITHUB_REPOSITORY: ${{ github.repository }}
108+
run: "$GITHUB_ACTION_PATH/scripts/prepare-keys.sh"
143109

144110
- name: Cache on S3
145111
uses: runs-on/cache@50350ad4242587b6c8c2baa2e740b1bc11285ff4 # v4.3.0

scripts/prepare-keys.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# This script prepares cache keys with branch-specific paths and fallback logic.
5+
# All inputs are passed through environment variables for security (sanitization).
6+
#
7+
# Required environment variables:
8+
# - INPUT_KEY: The primary cache key
9+
# - GITHUB_OUTPUT: File path for GitHub Actions output
10+
#
11+
# Optional environment variables:
12+
# - INPUT_RESTORE_KEYS: Multi-line list of restore key prefixes
13+
# - INPUT_FALLBACK_BRANCH: Maintenance branch for fallback restore keys (pattern: branch-*)
14+
# - GITHUB_HEAD_REF: Branch name for PR events
15+
# - GITHUB_REF: Branch ref for push events
16+
# - GITHUB_TOKEN: GitHub token for API authentication
17+
# - GITHUB_REPOSITORY: Repository in owner/repo format
18+
19+
# Use GITHUB_HEAD_REF for PR events, GITHUB_REF for push events
20+
BRANCH_NAME="${GITHUB_HEAD_REF:-$GITHUB_REF}"
21+
BRANCH_KEY="${BRANCH_NAME}/${INPUT_KEY}"
22+
echo "branch-key=${BRANCH_KEY}" >> "$GITHUB_OUTPUT"
23+
24+
# Process restore keys: keep branch-specific keys and add fallback to default branch
25+
if [ -n "${INPUT_RESTORE_KEYS:-}" ]; then
26+
RESTORE_KEYS=""
27+
28+
# First, add branch-specific restore keys
29+
while IFS= read -r line; do
30+
if [ -n "$line" ]; then
31+
if [ -n "$RESTORE_KEYS" ]; then
32+
RESTORE_KEYS="${RESTORE_KEYS}"$'\n'"${BRANCH_NAME}/${line}"
33+
else
34+
RESTORE_KEYS="${BRANCH_NAME}/${line}"
35+
fi
36+
fi
37+
done <<< "$INPUT_RESTORE_KEYS"
38+
39+
FALLBACK_BRANCH_INPUT="${INPUT_FALLBACK_BRANCH:-}"
40+
41+
if [[ -n "$FALLBACK_BRANCH_INPUT" ]]; then
42+
FALLBACK_BRANCH="${FALLBACK_BRANCH_INPUT#refs/heads/}"
43+
else
44+
# Query GitHub API to get the default branch
45+
FALLBACK_BRANCH=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
46+
"https://api.github.com/repos/${GITHUB_REPOSITORY}" | \
47+
jq -r '.default_branch')
48+
fi
49+
50+
if [[ -n "$FALLBACK_BRANCH" && "$FALLBACK_BRANCH" != "null" ]]; then
51+
case "$FALLBACK_BRANCH" in
52+
main|master|branch-*)
53+
# Add fallback branch restore keys
54+
while IFS= read -r line; do
55+
if [ -n "$line" ]; then
56+
RESTORE_KEYS="${RESTORE_KEYS}"$'\n'"refs/heads/${FALLBACK_BRANCH}/${line}"
57+
fi
58+
done <<< "$INPUT_RESTORE_KEYS"
59+
;;
60+
*)
61+
echo "::warning::Fallback branch '$FALLBACK_BRANCH' is not supported for cache fallback. Supported branches: main, master, branch-*"
62+
;;
63+
esac
64+
else
65+
echo "::warning::Unable to determine fallback branch; skipping fallback restore keys."
66+
fi
67+
68+
echo "branch-restore-keys<<EOF" >> "$GITHUB_OUTPUT"
69+
echo "$RESTORE_KEYS" >> "$GITHUB_OUTPUT"
70+
echo "EOF" >> "$GITHUB_OUTPUT"
71+
fi

0 commit comments

Comments
 (0)