action.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. # Helper function to handle release creation
  76. ensure_release() {
  77. local tag_name="$1"
  78. git fetch --tags --force
  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. if ! git rev-parse "$tag_name" >/dev/null 2>&1; then
  85. echo "Tag '$tag_name' not found. Creating backdated tag and release..."
  86. GIT_COMMITTER_DATE="2015-01-01T12:00:00" git tag -a "$tag_name" -m "Build Cache Storage"
  87. if git push origin "$tag_name" --force; then
  88. gh release create "$tag_name" --title "Build Cache" --notes "Automated storage for build assets" --latest=false --repo "$TARGET_REPO" || true
  89. gh release edit "$tag_name" --latest=false --repo "$TARGET_REPO" >/dev/null 2>&1 || true
  90. else
  91. echo "Push failed, tag might have been created by a parallel job. Continuing..."
  92. fi
  93. else
  94. echo "Tag '$tag_name' already exists but release is missing. Creating release..."
  95. gh release create "$tag_name" --title "Build Cache" --notes "Automated storage for build assets" --latest=false --repo "$TARGET_REPO" || true
  96. gh release edit "$tag_name" --latest=false --repo "$TARGET_REPO" >/dev/null 2>&1 || true
  97. fi
  98. }
  99. upload_with_retry() {
  100. local file=$1
  101. local tag_name=$2
  102. local max_attempts=3
  103. local timeout_duration="30m" # Increased timeout for large files
  104. for ((i=1; i<=max_attempts; i++)); do
  105. echo " Attempt $i for $file..."
  106. if timeout $timeout_duration gh release upload "$tag_name" "$file" --clobber --repo "$TARGET_REPO"; then
  107. echo " ✅ Successfully uploaded $file"
  108. return 0
  109. else
  110. local exit_code=$?
  111. if [ $exit_code -eq 124 ]; then
  112. echo " ⚠ Attempt $i timed out (timeout after $timeout_duration). Retrying..."
  113. else
  114. echo " ⚠ Attempt $i failed with exit code $exit_code. Retrying..."
  115. fi
  116. fi
  117. if [ $i -lt $max_attempts ]; then
  118. sleep 5
  119. fi
  120. done
  121. return 1
  122. }
  123. # Process each cache entry
  124. for ((idx=0; idx<${#PATHS_ARRAY[@]}; idx++)); do
  125. CACHE_PATH="${PATHS_ARRAY[$idx]}"
  126. CACHE_KEY="${KEYS_ARRAY[$idx]}"
  127. CACHE_BUCKET="${BUCKETS_ARRAY[$idx]}"
  128. [ -z "$CACHE_PATH" ] && continue
  129. TAG_NAME="$CACHE_BUCKET"
  130. BASE_FILENAME="cache-$CACHE_KEY"
  131. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  132. FILENAME="$BASE_FILENAME.tar"
  133. else
  134. FILENAME="$BASE_FILENAME.tzst"
  135. fi
  136. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Checking Release State ($CACHE_BUCKET)"
  137. ensure_release "$TAG_NAME"
  138. echo "::endgroup::"
  139. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Archiving Path: $CACHE_PATH"
  140. if [ ! -d "$CACHE_PATH" ] && [ ! -f "$CACHE_PATH" ]; then
  141. echo "⚠️ Target path not found. Skipping cache save."
  142. echo "::endgroup::"
  143. continue
  144. fi
  145. # Print concise summary: total size, top-level entries, and one-level-deep for largest entries
  146. summarize_dir() {
  147. local path="$1"
  148. local top_n=${2:-10}
  149. local sub_n=${3:-5}
  150. if [ ! -e "$path" ]; then
  151. echo "Path $path not found"
  152. return
  153. fi
  154. echo "--- Cache save summary for: $path ---"
  155. du -sh "$path" || true
  156. du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n $top_n || true
  157. echo "Showing one-level-deep details for top entries (up to $sub_n each):"
  158. while read -r line; do
  159. entry_path=$(echo "$line" | awk '{print $2}')
  160. [ -z "$entry_path" ] && continue
  161. if [ "$entry_path" = "$path" ]; then
  162. continue
  163. fi
  164. echo "-> $entry_path :"
  165. du -h --max-depth=1 "$entry_path" 2>/dev/null | sort -hr | head -n $sub_n || true
  166. done < <(du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n $top_n)
  167. echo "-------------------------------------"
  168. }
  169. summarize_dir "$CACHE_PATH" 10 5 || true
  170. DIR_NAME=$(dirname "$CACHE_PATH")
  171. BASE_NAME=$(basename "$CACHE_PATH")
  172. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  173. tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  174. else
  175. tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  176. fi
  177. FILE_SIZE=$(stat -c%s "$FILENAME")
  178. echo "Archive created: $FILENAME ($FILE_SIZE bytes)"
  179. # Create SHA256 checksum and upload alongside the archive/parts
  180. if command -v sha256sum >/dev/null 2>&1; then
  181. sha256sum "$FILENAME" | awk '{print $1}' > "$FILENAME.sha256"
  182. else
  183. # fallback using openssl
  184. openssl dgst -sha256 "$FILENAME" | awk '{print $2}' > "$FILENAME.sha256" || true
  185. fi
  186. echo "Checksum written: $FILENAME.sha256"
  187. echo "::endgroup::"
  188. echo "::group:: [$((idx+1))/${#PATHS_ARRAY[@]}] Uploading Assets ($CACHE_BUCKET)"
  189. MAX_SIZE=2000000000 # ~1.86GB
  190. # Check if cache has changed by comparing with previous release asset
  191. SKIP_UPLOAD=false
  192. if gh release view "$TAG_NAME" --repo "$TARGET_REPO" >/dev/null 2>&1; then
  193. echo "Checking if cache has changed..."
  194. # Try to get the size of the previous asset with the same name
  195. PREV_SIZE=$(gh release view "$TAG_NAME" --repo "$TARGET_REPO" --json assets --jq ".assets[] | select(.name == \"$FILENAME\") | .size" 2>/dev/null || echo "0")
  196. if [ "$PREV_SIZE" = "$FILE_SIZE" ]; then
  197. echo "⚠️ Cache size unchanged ($FILE_SIZE bytes). Skipping upload to save bandwidth."
  198. SKIP_UPLOAD=true
  199. fi
  200. fi
  201. if [ "$SKIP_UPLOAD" = "true" ]; then
  202. rm -f "$FILENAME"
  203. echo "::endgroup::"
  204. continue
  205. fi
  206. if [ "$FILE_SIZE" -gt "$MAX_SIZE" ]; then
  207. echo "⚠️ File > 2GB. Splitting into 1.5GB chunks..."
  208. split -b 1500M -a 2 "$FILENAME" "${FILENAME}.part"
  209. for part in "${FILENAME}".part*; do
  210. echo " Uploading $part..."
  211. if ! upload_with_retry "$part" "$TAG_NAME"; then
  212. echo "::warning::Failed to upload part $part after 3 attempts. Continuing with next cache..."
  213. fi
  214. rm -f "$part"
  215. done
  216. # Upload checksum for the original archive
  217. echo " Uploading checksum $FILENAME.sha256..."
  218. if ! upload_with_retry "$FILENAME.sha256" "$TAG_NAME"; then
  219. echo "::warning::Failed to upload checksum $FILENAME.sha256"
  220. fi
  221. rm -rf "$FILENAME"
  222. else
  223. echo " File size: $((FILE_SIZE / 1024 / 1024))MB"
  224. echo " Uploading $FILENAME..."
  225. if ! upload_with_retry "$FILENAME" "$TAG_NAME"; then
  226. echo "::warning::Failed to upload $FILENAME after 3 attempts. The cache will not be available for next run."
  227. fi
  228. # Upload checksum alongside the archive
  229. echo " Uploading checksum $FILENAME.sha256..."
  230. if ! upload_with_retry "$FILENAME.sha256" "$TAG_NAME"; then
  231. echo "::warning::Failed to upload checksum $FILENAME.sha256"
  232. fi
  233. rm -f "$FILENAME"
  234. rm -f "$FILENAME.sha256"
  235. fi
  236. echo "::endgroup::"
  237. done