action.yml 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. name: 'Upload Cache to GitHub Releases'
  2. description: 'Upload cache archive(s) to GitHub Releases with retry logic'
  3. inputs:
  4. cache_key:
  5. description: 'Unique key for the cache'
  6. required: true
  7. type: string
  8. cache_bucket:
  9. description: 'The tag name for the release'
  10. required: true
  11. default: 'general-cache'
  12. github_token:
  13. description: 'GitHub Token'
  14. required: true
  15. compression_level:
  16. description: 'Compression level (0-9)'
  17. required: false
  18. type: number
  19. default: 6
  20. debug:
  21. description: 'Enable Logs'
  22. required: false
  23. type: boolean
  24. default: false
  25. runs:
  26. using: 'composite'
  27. steps:
  28. - name: Upload Cache Asset(s)
  29. shell: bash
  30. env:
  31. GH_TOKEN: ${{ inputs.github_token }}
  32. TARGET_REPO: "${{ github.repository }}"
  33. run: |
  34. set -euo pipefail
  35. TAG_NAME="${{ inputs.cache_bucket }}"
  36. BASE_FILENAME="cache-${{ inputs.cache_key }}"
  37. if [ "${{ inputs.compression_level }}" -eq 0 ]; then
  38. FILENAME="$BASE_FILENAME.tar"
  39. else
  40. FILENAME="$BASE_FILENAME.tzst"
  41. fi
  42. upload_with_retry() {
  43. local file=$1
  44. local max_attempts=3
  45. local timeout_duration="10m"
  46. for ((i=1; i<=max_attempts; i++)); do
  47. echo " Attempt $i for $file..."
  48. if [ "${{ inputs.debug }}" = "true" ]; then
  49. if timeout "$timeout_duration" gh release upload "$TAG_NAME" "$file" --clobber --repo "$TARGET_REPO"; then
  50. echo " Successfully uploaded $file"
  51. return 0
  52. fi
  53. else
  54. if timeout "$timeout_duration" gh release upload "$TAG_NAME" "$file" --clobber --repo "$TARGET_REPO" > /dev/null 2>&1; then
  55. echo " Successfully uploaded $file"
  56. return 0
  57. fi
  58. fi
  59. echo " ⚠ Attempt $i failed. Retrying..."
  60. if [ "$i" -lt "$max_attempts" ]; then
  61. sleep 5
  62. fi
  63. done
  64. echo "::error::Failed to upload $file after multiple attempts."
  65. return 1
  66. }
  67. # Upload single file or split parts
  68. if [ -f "$FILENAME" ]; then
  69. if ! upload_with_retry "$FILENAME"; then
  70. exit 1
  71. fi
  72. rm -f "$FILENAME"
  73. else
  74. # Upload split parts
  75. for part in "${FILENAME}".part*; do
  76. if [ ! -f "$part" ]; then
  77. continue
  78. fi
  79. if ! upload_with_retry "$part"; then
  80. exit 1
  81. fi
  82. rm -f "$part"
  83. done
  84. rm -rf "$FILENAME"
  85. fi
  86. # Final cleanup
  87. rm -f "${BASE_FILENAME}"* || true
  88. echo "✅ Cache upload complete"