@@ -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
2931outputs :
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