action.yml 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. name: 'Save Cache'
  2. description: 'Archives and uploads cache to GitHub Releases. Supports both single and multiple cache paths/keys/buckets.'
  3. inputs:
  4. cache_path:
  5. description: 'Single path where cache is saved (use cache_paths for multiple)'
  6. required: false
  7. type: string
  8. cache_paths:
  9. description: 'Multiline list of paths where caches are saved (one per line)'
  10. required: false
  11. type: string
  12. default: ""
  13. cache_key:
  14. description: 'Unique key for the cache (use cache_keys for multiple)'
  15. required: false
  16. type: string
  17. cache_keys:
  18. description: 'Multiline list of unique keys for caches (one per line, must match cache_paths)'
  19. required: false
  20. type: string
  21. default: ""
  22. cache_bucket:
  23. description: 'Single tag name for the release (use cache_buckets for multiple)'
  24. required: false
  25. default: 'general-cache'
  26. cache_buckets:
  27. description: 'Multiline list of tag names for releases (one per line, must match cache_paths)'
  28. required: false
  29. type: string
  30. default: ""
  31. compression_level:
  32. description: 'Compression level (0-9). 0 = No compression, 1 = Fast, 19 = Best.'
  33. required: false
  34. type: number
  35. default: 6
  36. github_token:
  37. description: 'GitHub token for release upload authentication'
  38. required: true
  39. type: string
  40. runs:
  41. using: 'composite'
  42. steps:
  43. - name: Archive, Split, and Upload Cache
  44. shell: bash
  45. env:
  46. GITHUB_TOKEN: ${{ inputs.github_token }}
  47. TARGET_REPO: "${{ github.repository }}"
  48. run: |
  49. # Validate and parse inputs
  50. PATHS_INPUT="${{ inputs.cache_paths }}"
  51. KEYS_INPUT="${{ inputs.cache_keys }}"
  52. BUCKETS_INPUT="${{ inputs.cache_buckets }}"
  53. # If multiple inputs are empty, fall back to single inputs
  54. if [ -z "$PATHS_INPUT" ]; then
  55. PATHS_INPUT="${{ inputs.cache_path }}"
  56. fi
  57. if [ -z "$KEYS_INPUT" ]; then
  58. KEYS_INPUT="${{ inputs.cache_key }}"
  59. fi
  60. if [ -z "$BUCKETS_INPUT" ]; then
  61. BUCKETS_INPUT="${{ inputs.cache_bucket }}"
  62. fi
  63. # Split inputs into arrays
  64. IFS=$'\n' read -rd '' -a PATHS_ARRAY <<<"$PATHS_INPUT" || true
  65. IFS=$'\n' read -rd '' -a KEYS_ARRAY <<<"$KEYS_INPUT" || true
  66. IFS=$'\n' read -rd '' -a BUCKETS_ARRAY <<<"$BUCKETS_INPUT" || true
  67. # Validate array lengths match
  68. if [[ ${#PATHS_ARRAY[@]} -ne ${#KEYS_ARRAY[@]} ]] || [[ ${#PATHS_ARRAY[@]} -ne ${#BUCKETS_ARRAY[@]} ]]; then
  69. echo "::error::Mismatch in array lengths: paths=${#PATHS_ARRAY[@]}, keys=${#KEYS_ARRAY[@]}, buckets=${#BUCKETS_ARRAY[@]}"
  70. exit 1
  71. fi
  72. # Git configuration for releases
  73. git config user.name "github-actions[bot]"
  74. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  75. TARGET_COMMIT=$(git rev-parse HEAD)
  76. # Helper function to handle release creation
  77. ensure_release() {
  78. local tag_name="$1"
  79. if gh release view "$tag_name" --repo "$TARGET_REPO" >/dev/null 2>&1; then
  80. echo "Release '$tag_name' already exists. Ready for upload."
  81. gh release edit "$tag_name" --latest=false --repo "$TARGET_REPO" >/dev/null 2>&1 || true
  82. return 0
  83. fi
  84. echo "Tag '$tag_name' not found or release is missing. Creating release..."
  85. gh release create "$tag_name" \
  86. --target "$TARGET_COMMIT" \
  87. --title "Build Cache" \
  88. --notes "Automated storage for build assets" \
  89. --latest=false \
  90. --repo "$TARGET_REPO" || true
  91. gh release edit "$tag_name" --latest=false --repo "$TARGET_REPO" >/dev/null 2>&1 || true
  92. }
  93. upload_with_retry() {
  94. local file=$1
  95. local tag_name=$2
  96. local max_attempts=3
  97. local timeout_duration="30m" # Increased timeout for large files
  98. for ((i=1; i<=max_attempts; i++)); do
  99. echo " Attempt $i for $file..."
  100. if timeout $timeout_duration gh release upload "$tag_name" "$file" --clobber --repo "$TARGET_REPO"; then
  101. echo " ✅ Successfully uploaded $file"
  102. return 0
  103. else
  104. local exit_code=$?
  105. if [ $exit_code -eq 124 ]; then
  106. echo " ⚠ Attempt $i timed out (timeout after $timeout_duration). Retrying..."
  107. else
  108. echo " ⚠ Attempt $i failed with exit code $exit_code. Retrying..."
  109. fi
  110. fi
  111. if [ $i -lt $max_attempts ]; then
  112. sleep 5
  113. fi
  114. done
  115. return 1
  116. }
  117. # Process each cache entry
  118. for ((idx=0; idx<${#PATHS_ARRAY[@]}; idx++)); do
  119. CACHE_PATH="${PATHS_ARRAY[$idx]}"
  120. CACHE_KEY="${KEYS_ARRAY[$idx]}"
  121. CACHE_BUCKET="${BUCKETS_ARRAY[$idx]}"
  122. [ -z "$CACHE_PATH" ] && continue
  123. TAG_NAME="$CACHE_BUCKET"
  124. BASE_FILENAME="cache-$CACHE_KEY"
  125. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  126. FILENAME="$BASE_FILENAME.tar"
  127. else
  128. FILENAME="$BASE_FILENAME.tzst"
  129. fi
  130. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Checking Release State ($CACHE_BUCKET)"
  131. ensure_release "$TAG_NAME"
  132. echo "::endgroup::"
  133. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Archiving Path: $CACHE_PATH"
  134. if [ ! -d "$CACHE_PATH" ] && [ ! -f "$CACHE_PATH" ]; then
  135. echo "⚠️ Target path not found. Skipping cache save."
  136. echo "::endgroup::"
  137. continue
  138. fi
  139. # Print concise summary: total size, top-level entries, and one-level-deep for largest entries
  140. summarize_dir() {
  141. local path="$1"
  142. local top_n=${2:-10}
  143. local sub_n=${3:-5}
  144. if [ ! -e "$path" ]; then
  145. echo "Path $path not found"
  146. return
  147. fi
  148. echo "--- Cache save summary for: $path ---"
  149. du -sh "$path" || true
  150. du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n $top_n || true
  151. echo "Showing one-level-deep details for top entries (up to $sub_n each):"
  152. while read -r line; do
  153. entry_path=$(echo "$line" | awk '{print $2}')
  154. [ -z "$entry_path" ] && continue
  155. if [ "$entry_path" = "$path" ]; then
  156. continue
  157. fi
  158. echo "-> $entry_path :"
  159. du -h --max-depth=1 "$entry_path" 2>/dev/null | sort -hr | head -n $sub_n || true
  160. done < <(du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n $top_n)
  161. echo "-------------------------------------"
  162. }
  163. summarize_dir "$CACHE_PATH" 10 5 || true
  164. DIR_NAME=$(dirname "$CACHE_PATH")
  165. BASE_NAME=$(basename "$CACHE_PATH")
  166. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  167. tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  168. else
  169. tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  170. fi
  171. FILE_SIZE=$(stat -c%s "$FILENAME")
  172. echo "Archive created: $FILENAME ($FILE_SIZE bytes)"
  173. echo "::endgroup::"
  174. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Uploading Assets ($CACHE_BUCKET)"
  175. MAX_SIZE=2000000000 # ~1.86GB
  176. # Check if cache has changed by comparing with previous release asset
  177. SKIP_UPLOAD=false
  178. if gh release view "$TAG_NAME" --repo "$TARGET_REPO" >/dev/null 2>&1; then
  179. echo "Checking if cache has changed..."
  180. # Try to get the size of the previous asset with the same name
  181. PREV_SIZE=$(gh release view "$TAG_NAME" --repo "$TARGET_REPO" --json assets --jq ".assets[] | select(.name == \"$FILENAME\") | .size" 2>/dev/null || echo "0")
  182. if [ "$PREV_SIZE" = "$FILE_SIZE" ]; then
  183. echo "⚠️ Cache size unchanged ($FILE_SIZE bytes). Skipping upload to save bandwidth."
  184. SKIP_UPLOAD=true
  185. fi
  186. fi
  187. if [ "$SKIP_UPLOAD" = "true" ]; then
  188. rm -f "$FILENAME"
  189. echo "::endgroup::"
  190. continue
  191. fi
  192. if [ "$FILE_SIZE" -gt "$MAX_SIZE" ]; then
  193. echo "⚠️ File > 2GB. Splitting into 1.5GB chunks..."
  194. split -b 1500M -a 2 "$FILENAME" "${FILENAME}.part"
  195. for part in "${FILENAME}".part*; do
  196. echo " Uploading $part..."
  197. if ! upload_with_retry "$part" "$TAG_NAME"; then
  198. echo "::error::Failed to upload part $part after 3 attempts."
  199. exit 1
  200. fi
  201. rm -f "$part"
  202. done
  203. rm -rf "$FILENAME"
  204. else
  205. echo " File size: $((FILE_SIZE / 1024 / 1024))MB"
  206. echo " Uploading $FILENAME..."
  207. if ! upload_with_retry "$FILENAME" "$TAG_NAME"; then
  208. echo "::error::Failed to upload $FILENAME after 3 attempts."
  209. exit 1
  210. fi
  211. rm -f "$FILENAME"
  212. fi
  213. echo "::endgroup::"
  214. done