action.yml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. name: 'Restore Cache'
  2. inputs:
  3. cache_path:
  4. description: 'Path where cache should be restored'
  5. required: true
  6. type: string
  7. cache_key:
  8. description: 'Primary key to restore cache'
  9. required: true
  10. type: string
  11. restore_keys:
  12. description: 'Multiline list of keys (fallback prefixes)'
  13. required: false
  14. type: string
  15. default: ""
  16. cache_bucket:
  17. description: 'The tag name for the release (e.g., general-cache)'
  18. required: true
  19. default: 'general-cache'
  20. compression_level:
  21. description: 'Accepted for compatibility with callers; restore ignores this value.'
  22. required: false
  23. type: number
  24. default: 6
  25. debug:
  26. description: 'Enable Logs'
  27. required: false
  28. type: boolean
  29. default: false
  30. runs:
  31. using: 'composite'
  32. steps:
  33. - name: Detect and Download Cache
  34. shell: bash
  35. env:
  36. TARGET_REPO: "${{ github.repository }}"
  37. run: |
  38. # Detect and Download Cache
  39. if [ "${{ inputs.debug }}" = "true" ]; then set -x; fi
  40. cd "$GITHUB_WORKSPACE"
  41. TAG_NAME="${{ inputs.cache_bucket }}"
  42. BASE_URL="https://github.com/${TARGET_REPO}/releases/download/$TAG_NAME"
  43. ASSETS_URL="https://github.com/${TARGET_REPO}/releases/expanded_assets/$TAG_NAME"
  44. ARIA2_OPTS=(
  45. "-x16" "-s16" "-k1M" "-j5" "--file-allocation=none"
  46. "--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"
  47. "--header=Accept: */*"
  48. "--header=Connection: keep-alive"
  49. )
  50. if [ "${{ inputs.debug }}" != "true" ]; then
  51. ARIA2_OPTS+=("--quiet" "--summary-interval=0" "--console-log-level=error")
  52. fi
  53. echo "::group:: Fetching Asset List"
  54. HTTP_RESPONSE=$(curl -sL -A "Mozilla/5.0" -w "%{http_code}" "$ASSETS_URL" -o assets.html || echo "404")
  55. if [ "$HTTP_RESPONSE" -eq 200 ]; then
  56. ALL_ASSETS=$(grep -oP "download/$TAG_NAME/\K[^\"' ]+" assets.html | sort -u || echo "")
  57. else
  58. echo "Status $HTTP_RESPONSE: Failed to fetch assets from scraper endpoint."
  59. ALL_ASSETS=""
  60. fi
  61. rm -f assets.html
  62. if [ -z "$ALL_ASSETS" ]; then
  63. echo "No assets found in bucket '$TAG_NAME'. Skipping search."
  64. echo "::endgroup::"
  65. else
  66. echo "Successfully fetched asset list. Has $(echo "$ALL_ASSETS" | wc -l) assets."
  67. echo "::endgroup::"
  68. SEARCH_LIST=$(printf "%s\n%s" "${{ inputs.cache_key }}" "${{ inputs.restore_keys }}" | sed '/^$/d')
  69. FOUND=false
  70. while read -r KEY; do
  71. [ -z "$KEY" ] && continue
  72. echo "::group:: Searching Prefix: $KEY"
  73. MATCH=$(echo "$ALL_ASSETS" | grep "^cache-$KEY" | sort -r | head -n 1 || true)
  74. if [ -z "$MATCH" ]; then
  75. echo "Not found."
  76. echo "::endgroup::"
  77. continue
  78. fi
  79. BASE_FILENAME=$(echo "$MATCH" | sed -E 's/\.(tzst|tar)(\.part[a-z]{2})?$//')
  80. EXT=$(echo "$MATCH" | grep -oP '\\.(tzst|tar)' | head -n 1)
  81. [[ "$MATCH" == *".part"* ]] && IS_SPLIT=true || IS_SPLIT=false
  82. [ "$EXT" = ".tzst" ] && COMPRESSED=true || COMPRESSED=false
  83. FOUND=true
  84. echo "✅ Hit! Restoring $BASE_FILENAME$EXT"
  85. echo "::endgroup::"
  86. echo "::group:: Downloading Cache"
  87. download_asset() {
  88. local filename="$1"
  89. local url="$2"
  90. local max_retries=3
  91. local attempt=1
  92. while [ $attempt -le $max_retries ]; do
  93. echo " Attempt $attempt: Downloading $filename..."
  94. if timeout 10m aria2c "${ARIA2_OPTS[@]}" --retry-wait=10 --max-tries=10 -o "$filename" "$url"; then
  95. return 0
  96. fi
  97. echo " ⚠️ Download failed or timed out. Retrying in 5s..."
  98. sleep 5
  99. attempt=$((attempt + 1))
  100. done
  101. return 1
  102. }
  103. if [ "$IS_SPLIT" = "true" ]; then
  104. for part in {a..z}{a..z}; do
  105. PART_NAME="$BASE_FILENAME$EXT.part$part"
  106. if echo "$ALL_ASSETS" | grep -q "^$PART_NAME$"; then
  107. if ! download_asset "$PART_NAME" "$BASE_URL/$PART_NAME"; then
  108. echo "::error::Failed to download $PART_NAME after multiple retries."
  109. exit 1
  110. fi
  111. else break; fi
  112. done
  113. else
  114. if ! download_asset "$BASE_FILENAME$EXT" "$BASE_URL/$BASE_FILENAME$EXT"; then
  115. echo "::error::Failed to download $BASE_FILENAME$EXT after multiple retries."
  116. exit 1
  117. fi
  118. fi
  119. echo "::endgroup::"
  120. echo "::group:: Extracting Cache"
  121. mkdir -p "${{ inputs.cache_path }}"
  122. FINAL_ARCHIVE="$BASE_FILENAME$EXT"
  123. EXTRACT_DIR="$(dirname "${{ inputs.cache_path }}")"
  124. extract_cache() {
  125. local cmd="$1"
  126. if [ "${{ inputs.debug }}" = "true" ]; then
  127. sh -c "$cmd"
  128. else
  129. sh -c "$cmd" > /dev/null 2>&1
  130. fi
  131. }
  132. if [ "$IS_SPLIT" = "true" ]; then
  133. PARTS=$(ls "$FINAL_ARCHIVE.part"* | sort)
  134. if [ "$COMPRESSED" = "true" ]; then
  135. extract_cache "cat $PARTS | tar -I 'zstd -d -T0' -xvf - -C '$EXTRACT_DIR'"
  136. else
  137. extract_cache "cat $PARTS | tar -xvf - -C '$EXTRACT_DIR'"
  138. fi
  139. rm -f $PARTS
  140. else
  141. if [ "$COMPRESSED" = "true" ]; then
  142. extract_cache "tar -I 'zstd -d -T0' -xvf '$FINAL_ARCHIVE' -C '$EXTRACT_DIR'"
  143. else
  144. extract_cache "tar -xvf '$FINAL_ARCHIVE' -C '$EXTRACT_DIR'"
  145. fi
  146. rm -f "$FINAL_ARCHIVE"
  147. fi
  148. echo "✅ Restore complete."
  149. echo "::endgroup::"
  150. break
  151. done <<< "$SEARCH_LIST"
  152. if [ "$FOUND" = "false" ]; then
  153. echo "⚠️ No cache matches found. Proceeding with fresh run."
  154. fi
  155. fi