action.yml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. name: 'Restore Cache'
  2. description: 'Restores cache from GitHub Releases. Supports both single and multiple cache paths/keys/buckets.'
  3. inputs:
  4. cache_path:
  5. description: 'Single path where cache should be restored (use cache_paths for multiple)'
  6. required: false
  7. type: string
  8. cache_paths:
  9. description: 'Multiline list of paths where caches should be restored (one per line)'
  10. required: false
  11. type: string
  12. default: ""
  13. cache_key:
  14. description: 'Primary key to restore cache (use cache_keys for multiple)'
  15. required: false
  16. type: string
  17. cache_keys:
  18. description: 'Multiline list of primary keys to restore caches (one per line, must match cache_paths)'
  19. required: false
  20. type: string
  21. default: ""
  22. restore_keys:
  23. description: 'Multiline list of keys (fallback prefixes)'
  24. required: false
  25. type: string
  26. default: ""
  27. cache_bucket:
  28. description: 'Single tag name for the release (use cache_buckets for multiple)'
  29. required: false
  30. default: 'general-cache'
  31. cache_buckets:
  32. description: 'Multiline list of tag names for releases (one per line, must match cache_paths)'
  33. required: false
  34. type: string
  35. default: ""
  36. compression_level:
  37. description: 'Compression level (0-9). 0 = No compression, 1 = Fast, 19 = Best.'
  38. required: false
  39. type: number
  40. default: 6
  41. outputs:
  42. cache-hit:
  43. description: 'Returns "true" if at least one cache was found and restored, "false" otherwise'
  44. value: ${{ steps.restore.outputs.cache-hit }}
  45. cache-key:
  46. description: 'The key that matched (primary or fallback). Multiple entries joined by comma.'
  47. value: ${{ steps.restore.outputs.cache-key }}
  48. cache-size:
  49. description: 'Size of restored cache in human-readable format. Multiple entries joined by comma.'
  50. value: ${{ steps.restore.outputs.cache-size }}
  51. runs:
  52. using: 'composite'
  53. steps:
  54. - name: Detect and Download Cache
  55. id: restore
  56. shell: bash
  57. working-directory: ${{ github.workspace }}
  58. env:
  59. TARGET_REPO: "${{ github.repository }}"
  60. run: |
  61. # Validate and parse inputs
  62. PATHS_INPUT="${{ inputs.cache_paths }}"
  63. KEYS_INPUT="${{ inputs.cache_keys }}"
  64. BUCKETS_INPUT="${{ inputs.cache_buckets }}"
  65. # If multiple inputs are empty, fall back to single inputs
  66. if [ -z "$PATHS_INPUT" ]; then
  67. PATHS_INPUT="${{ inputs.cache_path }}"
  68. fi
  69. if [ -z "$KEYS_INPUT" ]; then
  70. KEYS_INPUT="${{ inputs.cache_key }}"
  71. fi
  72. if [ -z "$BUCKETS_INPUT" ]; then
  73. BUCKETS_INPUT="${{ inputs.cache_bucket }}"
  74. fi
  75. # Split inputs into arrays
  76. IFS=$'\n' read -rd '' -a PATHS_ARRAY <<<"$PATHS_INPUT" || true
  77. IFS=$'\n' read -rd '' -a KEYS_ARRAY <<<"$KEYS_INPUT" || true
  78. IFS=$'\n' read -rd '' -a BUCKETS_ARRAY <<<"$BUCKETS_INPUT" || true
  79. # Validate array lengths match
  80. if [[ ${#PATHS_ARRAY[@]} -ne ${#KEYS_ARRAY[@]} ]] || [[ ${#PATHS_ARRAY[@]} -ne ${#BUCKETS_ARRAY[@]} ]]; then
  81. echo "::error::Mismatch in array lengths: paths=${#PATHS_ARRAY[@]}, keys=${#KEYS_ARRAY[@]}, buckets=${#BUCKETS_ARRAY[@]}"
  82. exit 1
  83. fi
  84. ARIA2_OPTS=(
  85. "-x16" "-s16" "-k1M" "-j5" "--file-allocation=none"
  86. "--header=User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  87. "--header=Accept: */*"
  88. "--header=Connection: keep-alive"
  89. )
  90. ARIA2_OPTS+=("--quiet" "--summary-interval=0" "--console-log-level=error")
  91. # Collect results
  92. OVERALL_HIT=false
  93. MATCHED_KEYS=()
  94. CACHE_SIZES=()
  95. # Process each cache entry
  96. for ((idx=0; idx<${#PATHS_ARRAY[@]}; idx++)); do
  97. CACHE_PATH="${PATHS_ARRAY[$idx]}"
  98. CACHE_KEY="${KEYS_ARRAY[$idx]}"
  99. CACHE_BUCKET="${BUCKETS_ARRAY[$idx]}"
  100. [ -z "$CACHE_PATH" ] && continue
  101. TAG_NAME="$CACHE_BUCKET"
  102. BASE_URL="https://github.com/${TARGET_REPO}/releases/download/$TAG_NAME"
  103. ASSETS_URL="https://github.com/${TARGET_REPO}/releases/expanded_assets/$TAG_NAME"
  104. echo "::group:: [$((idx+1))/$${#PATHS_ARRAY[@]}] Cache: $CACHE_BUCKET - Fetching Asset List"
  105. HTTP_RESPONSE=$(curl -sL -A "Mozilla/5.0" -w "%{http_code}" "$ASSETS_URL" -o assets.html || echo "404")
  106. if [ "$HTTP_RESPONSE" -eq 200 ]; then
  107. ALL_ASSETS=$(grep -oP "download/$TAG_NAME/\K[^\"' ]+" assets.html | sort -u || echo "")
  108. else
  109. echo "Status $HTTP_RESPONSE: Failed to fetch assets from scraper endpoint."
  110. ALL_ASSETS=""
  111. fi
  112. rm -f assets.html
  113. if [ -z "$ALL_ASSETS" ]; then
  114. echo "No assets found in bucket '$TAG_NAME'. Skipping."
  115. echo "::endgroup::"
  116. continue
  117. fi
  118. echo "Successfully fetched asset list. Has $(echo "$ALL_ASSETS" | wc -l) assets."
  119. echo "::endgroup::"
  120. SEARCH_LIST=$(printf "%s\n%s" "$CACHE_KEY" "${{ inputs.restore_keys }}" | sed '/^$/d')
  121. FOUND=false
  122. while read -r KEY; do
  123. [ -z "$KEY" ] && continue
  124. echo "::group:: [$((idx+1))/$${#PATHS_ARRAY[@]}] Searching Prefix: $KEY"
  125. MATCH=$(echo "$ALL_ASSETS" | grep "^cache-$KEY" | sort -r | head -n 1 || true)
  126. if [ -z "$MATCH" ]; then
  127. echo "Not found."
  128. echo "::endgroup::"
  129. continue
  130. fi
  131. BASE_FILENAME=$(echo "$MATCH" | sed -E 's/\.(tzst|tar)(\.part[a-z]{2})?$//')
  132. EXT=$(echo "$MATCH" | grep -oP '\.(tzst|tar)' | head -n 1)
  133. [[ "$MATCH" == *".part"* ]] && IS_SPLIT=true || IS_SPLIT=false
  134. [ "$EXT" = ".tzst" ] && COMPRESSED=true || COMPRESSED=false
  135. FOUND=true
  136. echo "✅ Hit! Restoring $BASE_FILENAME$EXT"
  137. echo "::endgroup::"
  138. echo "::group:: [$((idx+1))/$${#PATHS_ARRAY[@]}] Downloading Cache"
  139. download_asset() {
  140. local filename="$1"
  141. local url="$2"
  142. local max_retries=3
  143. local attempt=1
  144. while [ $attempt -le $max_retries ]; do
  145. echo " Attempt $attempt: Downloading $filename..."
  146. if timeout 10m aria2c "${ARIA2_OPTS[@]}" --retry-wait=10 --max-tries=10 -o "$filename" "$url"; then
  147. return 0
  148. fi
  149. echo " ⚠️ Download failed or timed out. Retrying in 5s..."
  150. sleep 5
  151. attempt=$((attempt + 1))
  152. done
  153. return 1
  154. }
  155. if [ "$IS_SPLIT" = "true" ]; then
  156. for part in {a..z}{a..z}; do
  157. PART_NAME="$BASE_FILENAME$EXT.part$part"
  158. if echo "$ALL_ASSETS" | grep -q "^$PART_NAME$"; then
  159. if ! download_asset "$PART_NAME" "$BASE_URL/$PART_NAME"; then
  160. echo "::error::Failed to download $PART_NAME after multiple retries."
  161. exit 1
  162. fi
  163. else break; fi
  164. done
  165. else
  166. if ! download_asset "$BASE_FILENAME$EXT" "$BASE_URL/$BASE_FILENAME$EXT"; then
  167. echo "::error::Failed to download $BASE_FILENAME$EXT after multiple retries."
  168. exit 1
  169. fi
  170. fi
  171. echo "::endgroup::"
  172. echo "::group:: [$((idx+1))/$${#PATHS_ARRAY[@]}] Extracting Cache"
  173. mkdir -p "$CACHE_PATH"
  174. FINAL_ARCHIVE="$BASE_FILENAME$EXT"
  175. EXTRACT_DIR="$(dirname "$CACHE_PATH")"
  176. extract_cache() {
  177. local cmd="$1"
  178. sh -c "$cmd" > /dev/null 2>&1
  179. }
  180. if [ "$IS_SPLIT" = "true" ]; then
  181. PARTS=$(ls "$FINAL_ARCHIVE.part"* | sort)
  182. if [ "$COMPRESSED" = "true" ]; then
  183. extract_cache "cat $PARTS | tar -I 'zstd -d -T0' -xvf - -C '$EXTRACT_DIR'"
  184. else
  185. extract_cache "cat $PARTS | tar -xvf - -C '$EXTRACT_DIR'"
  186. fi
  187. rm -f $PARTS
  188. else
  189. if [ "$COMPRESSED" = "true" ]; then
  190. extract_cache "tar -I 'zstd -d -T0' -xvf '$FINAL_ARCHIVE' -C '$EXTRACT_DIR'"
  191. else
  192. extract_cache "tar -xvf '$FINAL_ARCHIVE' -C '$EXTRACT_DIR'"
  193. fi
  194. rm -f "$FINAL_ARCHIVE"
  195. fi
  196. echo "✅ Restore complete."
  197. echo "::endgroup::"
  198. # Track results for this entry
  199. OVERALL_HIT=true
  200. MATCHED_KEYS+=("$KEY")
  201. CACHE_SIZE=$(du -sh "$CACHE_PATH" 2>/dev/null | awk '{print $1}' || echo "unknown")
  202. CACHE_SIZES+=("$CACHE_SIZE")
  203. break
  204. done <<< "$SEARCH_LIST"
  205. if [ "$FOUND" = "false" ]; then
  206. echo "⚠️ No cache matches found for bucket '$CACHE_BUCKET'. Proceeding with fresh run."
  207. MATCHED_KEYS+=("none")
  208. CACHE_SIZES+=("0B")
  209. fi
  210. done
  211. # Output results
  212. if [ "$OVERALL_HIT" = "true" ]; then
  213. echo "cache-hit=true" >> $GITHUB_OUTPUT
  214. else
  215. echo "cache-hit=false" >> $GITHUB_OUTPUT
  216. fi
  217. echo "cache-key=$(IFS=,; echo "${MATCHED_KEYS[*]}")" >> $GITHUB_OUTPUT
  218. echo "cache-size=$(IFS=,; echo "${CACHE_SIZES[*]}")" >> $GITHUB_OUTPUT