Skip to content

Commit 94cec8f

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

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

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 branch used for fallback restore keys (must be `main`, `master`, or `branch-*`) | 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: 32 additions & 18 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 branch ref used for fallback restore keys (must be main, master, or branch-*)
2830

2931
outputs:
3032
cache-hit:
@@ -117,24 +119,36 @@ runs:
117119
fi
118120
done <<< "${{ inputs.restore-keys }}"
119121
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
122+
# Determine branch to use for fallback restore keys
123+
FALLBACK_BRANCH_INPUT="${{ inputs.fallback-branch }}"
124+
125+
if [[ -n "$FALLBACK_BRANCH_INPUT" ]]; then
126+
if [[ "$FALLBACK_BRANCH_INPUT" == refs/heads/* ]]; then
127+
FALLBACK_BRANCH_INPUT="${FALLBACK_BRANCH_INPUT#refs/heads/}"
128+
fi
129+
FALLBACK_BRANCH="$FALLBACK_BRANCH_INPUT"
130+
else
131+
FALLBACK_BRANCH=$(curl -s -H "Authorization: token ${{ github.token }}" \
132+
"https://api.github.com/repos/${{ github.repository }}" | \
133+
jq -r '.default_branch')
134+
fi
135+
136+
if [[ -z "$FALLBACK_BRANCH" || "$FALLBACK_BRANCH" == "null" ]]; then
137+
echo "::warning::Unable to determine fallback branch; skipping fallback restore keys."
138+
else
139+
case "$FALLBACK_BRANCH" in
140+
main|master|branch-*)
141+
while IFS= read -r line; do
142+
if [ -n "$line" ]; then
143+
RESTORE_KEYS="${RESTORE_KEYS}"$'\n'"refs/heads/${FALLBACK_BRANCH}/${line}"
144+
fi
145+
done <<< "${{ inputs.restore-keys }}"
146+
;;
147+
*)
148+
echo "::warning::Fallback branch '$FALLBACK_BRANCH' is not supported for cache fallback. Supported branches: main, master, branch-*"
149+
;;
150+
esac
151+
fi
138152
139153
echo "branch-restore-keys<<EOF" >> $GITHUB_OUTPUT
140154
echo "$RESTORE_KEYS" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)