cdn_upload 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. set -e
  3. function upload_with_content_type() {
  4. content_type=$1
  5. bucket=$2
  6. shift 2
  7. content_type_options=""
  8. if [[ "$content_type" != "-" ]]; then
  9. content_type_options="-h Content-Type:${content_type};charset=utf-8"
  10. fi
  11. # DOCS for gsutil -- it does not have long command line flags!
  12. ## global flags
  13. # -h NAME:VALUE add header, can occur multiples times
  14. # -m upload with multiple threads
  15. ## rsync flags
  16. # -r traverse into directories recursively
  17. # -x Python regex for excluding files from the sync
  18. gsutil \
  19. -h "Cache-Control:public, max-age=31536000" \
  20. ${content_type_options} \
  21. -m \
  22. rsync \
  23. -r \
  24. "$@" \
  25. "/tmp/public/" \
  26. "${bucket}/public/"
  27. }
  28. function upload_into_bucket() {
  29. bucket=$1
  30. # stylesheets
  31. upload_with_content_type 'text/css' "$bucket" \
  32. -x '.+(?<!\.css)$'
  33. # javascript files
  34. upload_with_content_type 'application/javascript' "$bucket" \
  35. -x '.+(?<!\.js)$'
  36. # the rest
  37. upload_with_content_type '-' "$bucket" \
  38. -x '.+\.(css|js)$'
  39. }
  40. # Upload to staging CDN if branch is either 'master' or 'staging-master' or main variants
  41. if [[ "$BRANCH_NAME" == "master" || "$BRANCH_NAME" == "staging-master" || "$BRANCH_NAME" == "main" || "$BRANCH_NAME" == "staging-main" ]]; then
  42. tar --directory=/tmp/ -xf build.tar
  43. # delete source maps
  44. find /tmp/public -name '*.js.map' -delete
  45. upload_into_bucket $CDN_STAG
  46. # Only upload to production CDN if branch is
  47. if [[ "$BRANCH_NAME" == "master" || "$BRANCH_NAME" == "main" ]]; then
  48. upload_into_bucket $CDN_PROD
  49. fi
  50. fi