action.yml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. name: 'Save Cache'
  2. description: ''
  3. inputs:
  4. cache_path:
  5. description: 'Path where cache is saved'
  6. required: true
  7. type: string
  8. cache_key:
  9. description: 'Unique key for the cache'
  10. required: true
  11. type: string
  12. cache_bucket:
  13. description: 'The tag name for the release (e.g., general-cache)'
  14. required: true
  15. default: 'general-cache'
  16. github_token:
  17. description: 'GitHub Token'
  18. required: true
  19. compression_level:
  20. description: 'Compression level (0-9). 0 = No compression, 1 = Fast, 19 = Best.'
  21. required: false
  22. type: number
  23. default: 6
  24. working_dir:
  25. description: 'Path where .git folder exists (use only if you clone multiple repos)'
  26. required: false
  27. type: string
  28. debug:
  29. description: 'Enable Logs'
  30. required: false
  31. type: boolean
  32. default: false
  33. runs:
  34. using: 'composite'
  35. steps:
  36. - name: Archive, Split, and Upload Cache
  37. shell: bash
  38. env:
  39. GH_TOKEN: ${{ inputs.github_token }}
  40. TARGET_REPO: "${{ github.repository }}"
  41. run: |
  42. set -euo pipefail
  43. # Archive, Split, and Upload Cache
  44. if [ "${{ inputs.debug }}" = "true" ]; then set -x; fi
  45. if [ "${{ inputs.working_dir }}" != "" ]; then
  46. cd "${{ inputs.working_dir }}"
  47. fi
  48. git config user.name "github-actions[bot]"
  49. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  50. TAG_NAME="${{ inputs.cache_bucket }}"
  51. BASE_FILENAME="cache-${{ inputs.cache_key }}"
  52. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  53. FILENAME="$BASE_FILENAME.tar"
  54. else
  55. FILENAME="$BASE_FILENAME.tzst"
  56. fi
  57. echo "::group:: Checking Release State"
  58. git fetch --tags --force
  59. if ! git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
  60. echo "Tag '$TAG_NAME' not found. Creating backdated release..."
  61. GIT_COMMITTER_DATE="2015-01-01T12:00:00" git tag -a "$TAG_NAME" -m "General Cache Storage"
  62. if git push origin "$TAG_NAME" --force; then
  63. gh release create "$TAG_NAME" --title "General Cache" --notes "Automated storage for build assets" --repo "$TARGET_REPO" || true
  64. else
  65. echo "Push failed, tag might have been created by a parallel job. Continuing..."
  66. fi
  67. else
  68. echo "Release '$TAG_NAME' already exists. Ready for upload."
  69. fi
  70. echo "::endgroup::"
  71. echo "::group:: Archiving Path"
  72. echo "Target: ${{ inputs.cache_path }}"
  73. if [ ! -d "${{ inputs.cache_path }}" ] && [ ! -f "${{ inputs.cache_path }}" ]; then
  74. echo "⚠️ Target path not found. Skipping cache save."
  75. echo "::endgroup::"
  76. else
  77. DIR_NAME=$(dirname "${{ inputs.cache_path }}")
  78. BASE_NAME=$(basename "${{ inputs.cache_path }}")
  79. if [ "${{ inputs.debug }}" = "true" ]; then
  80. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  81. tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME"
  82. else
  83. tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME"
  84. fi
  85. else
  86. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  87. tar --posix -h -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  88. else
  89. tar --posix -h -I "zstd -T0 -${{ inputs.compression_level }}" -cvf "$FILENAME" -C "$DIR_NAME" "$BASE_NAME" > /dev/null 2>&1
  90. fi
  91. fi
  92. FILE_SIZE=$(stat -c%s "$FILENAME")
  93. echo "Archive created: $FILENAME ($FILE_SIZE bytes)"
  94. echo "::endgroup::"
  95. echo "::group:: Uploading Assets"
  96. MAX_SIZE=2000000000 # ~1.86GB
  97. upload_with_retry() {
  98. local file=$1
  99. local max_attempts=3
  100. local timeout_duration="10m"
  101. if [ "$TAG_NAME" != "bazel-cache" ]; then
  102. timeout_duration="30m"
  103. fi
  104. for ((i=1; i<=max_attempts; i++)); do
  105. echo " Attempt $i for $file..."
  106. if [ "${{ inputs.debug }}" = "true" ]; then
  107. if timeout "$timeout_duration" gh release upload "$TAG_NAME" "$file" --clobber --repo "$TARGET_REPO"; then
  108. echo " Successfully uploaded $file"
  109. return 0
  110. fi
  111. else
  112. if timeout "$timeout_duration" gh release upload "$TAG_NAME" "$file" --clobber --repo "$TARGET_REPO" > /dev/null 2>&1; then
  113. echo " Successfully uploaded $file"
  114. return 0
  115. fi
  116. fi
  117. echo " ⚠ Attempt $i failed or timed out after $timeout_duration. Retrying..."
  118. if [ "$i" -lt "$max_attempts" ]; then
  119. sleep 5
  120. fi
  121. done
  122. return 1
  123. }
  124. chunk_size="1500M"
  125. split_required=false
  126. if [ "$TAG_NAME" != "bazel-cache" ]; then
  127. chunk_size="250M"
  128. split_required=true
  129. elif [ "$FILE_SIZE" -gt "$MAX_SIZE" ]; then
  130. split_required=true
  131. fi
  132. if [ "$split_required" = "true" ]; then
  133. echo "⚠ Splitting into ${chunk_size} chunks..."
  134. split -b "$chunk_size" -a 2 "$FILENAME" "${FILENAME}.part"
  135. for part in "${FILENAME}".part*; do
  136. echo "Uploading $part..."
  137. if ! upload_with_retry "$part"; then
  138. echo "::error::Failed to upload part $part after multiple attempts."
  139. exit 1
  140. fi
  141. rm -f "$part"
  142. done
  143. rm -rf "$FILENAME"
  144. else
  145. echo "Uploading $FILENAME..."
  146. if ! upload_with_retry "$FILENAME"; then
  147. echo "::error::Failed to upload $FILENAME after multiple attempts."
  148. exit 1
  149. fi
  150. rm -f "$FILENAME"
  151. fi
  152. # Cleanup any leftover archive artifacts (single-file or split parts)
  153. if [ "${{ inputs.debug }}" = "true" ]; then
  154. echo "Cleaning archive artifacts: ${BASE_FILENAME}*"
  155. fi
  156. rm -f "${BASE_FILENAME}"* || true
  157. echo "::endgroup::"
  158. fi