cdn_upload 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set -eEu
  3. function upload_into_bucket() {
  4. bucket=$1
  5. # stylesheets
  6. bin/cdn_upload_batch 'text/css' "$bucket" '.css' \
  7. -x '.+(?<!\.css)$' &
  8. # javascript files
  9. bin/cdn_upload_batch 'application/javascript' "$bucket" '.js' \
  10. -x '.+(?<!\.js)$' &
  11. # the rest
  12. bin/cdn_upload_batch '-' "$bucket" '-' \
  13. -x '.+\.(css|js)$' &
  14. wait
  15. }
  16. verify_upload_into_bucket() {
  17. local bucket
  18. local missing_from_bucket
  19. bucket=$1
  20. printf '\nINFO: Verifying file availability in %s.\n' "$bucket"
  21. readarray -t missing_from_bucket < <(
  22. comm -13 \
  23. <(gsutil ls "${bucket}/public/**" | sed "s@${bucket}/@@" | sort) \
  24. <(find /tmp/public /tmp/compressed -type f | sed '
  25. # Remove absolute path prefix
  26. s@^/tmp/@@;
  27. # Undo the compressed/ directory separation that does not exist in the bucket
  28. s@^compressed/@@
  29. ' | sort)
  30. )
  31. if [[ ${#missing_from_bucket[@]} -eq 0 ]]; then
  32. printf 'INFO: Verification successful: all local files have been found in bucket %s.\n' \
  33. "$bucket"
  34. else
  35. printf >&2 'WARN: %d local file(s) not available in bucket %s:\n' \
  36. ${#missing_from_bucket[@]} "$bucket"
  37. printf >&2 ' - %s\n' "${missing_from_bucket[@]}"
  38. return 1
  39. fi
  40. }
  41. # Upload to staging CDN if branch is either 'main' or 'staging-main'
  42. if [[ "$BRANCH_NAME" == "main" ]] || [[ "$BRANCH_NAME" == "staging-main" ]]; then
  43. tar --directory=/tmp/ -xf build.tar
  44. # delete source maps
  45. find /tmp/public -name '*.js.map' -delete
  46. bin/compress_assets
  47. upload_into_bucket "$CDN_STAG" &&
  48. verify_upload_into_bucket "$CDN_STAG" || exit 3
  49. # Only upload to production CDN if branch is
  50. if [[ "$BRANCH_NAME" == "main" ]]; then
  51. upload_into_bucket "$CDN_PROD" &&
  52. verify_upload_into_bucket "$CDN_PROD" || exit 3
  53. fi
  54. fi