upload-to-cdn.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. # Upload Uppy releases to Edgly.net CDN. Copyright (c) 2018, Transloadit Ltd.
  3. #
  4. # This file:
  5. #
  6. # - Assumes EDGLY_KEY and EDGLY_SECRET are available (e.g. set via Travis secrets)
  7. # - Tries to load env.sh instead, if not
  8. # - Checks if a tag is being built (on Travis - otherwise opts to continue execution regardless)
  9. # - Installs AWS CLI if needed
  10. # - Assumed a fully built uppy is in root dir (unless a specific tag was specified, then it's fetched from npm)
  11. # - Runs npm pack, and stores files to e.g. https://transloadit.edgly.net/releases/uppy/v0.22.4/dist/uppy.css
  12. # - Uses local package by default, if [version] argument was specified, takes package from npm
  13. #
  14. # Run as:
  15. #
  16. # ./upload-to-cdn.sh [version]
  17. #
  18. # To upload all versions in one go (DANGER):
  19. #
  20. # git tag |awk -Fv '{print "./bin/upload-to-cdn.sh "$2}' |bash
  21. #
  22. # Authors:
  23. #
  24. # - Kevin van Zonneveld <kevin@transloadit.com>
  25. set -o pipefail
  26. set -o errexit
  27. set -o nounset
  28. # set -o xtrace
  29. # Set magic variables for current FILE & DIR
  30. __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  31. __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
  32. __base="$(basename ${__file} .sh)"
  33. __root="$(cd "$(dirname "${__dir}")" && pwd)"
  34. function fatal () {
  35. echo "❌ ${*}";
  36. exit 1
  37. }
  38. pushd "${__root}" > /dev/null 2>&1
  39. if [ "${TRAVIS:-}" = "true" ]; then
  40. if [ "${TRAVIS_PULL_REQUEST:-}" != "false" ]; then
  41. echo "On Travis (TRAVIS is '${TRAVIS}'), I'm not pushing releases to the CDN for pull requests (TRAVIS_PULL_REQUEST is '${TRAVIS_PULL_REQUEST}')"
  42. exit 0
  43. fi
  44. if [ -z "${TRAVIS_TAG:-}" ]; then
  45. echo "On Travis (TRAVIS is '${TRAVIS}'), I'm only pushing releases to the CDN when a tag is being built (TRAVIS_TAG is '${TRAVIS_TAG}')"
  46. exit 0
  47. fi
  48. fi
  49. if [ -z "${EDGLY_KEY:-}" ] && [ -f ./env.sh ]; then
  50. source ./env.sh
  51. fi
  52. [ -z "${EDGLY_KEY:-}" ] && fatal "Unable to find or source EDGLY_KEY env var"
  53. type aws || pip install --user awscli
  54. remoteVersion="${1:-}"
  55. version="${remoteVersion}"
  56. if [ -z "${remoteVersion}" ]; then
  57. localVersion=$(node -pe "require('./package.json').version")
  58. echo "${localVersion}"
  59. version="${localVersion}"
  60. fi
  61. majorVersion=$(echo "${version}" |awk -F. '{print $1}')
  62. echo -n "--> Check if not overwriting an existing tag ... "
  63. env \
  64. AWS_ACCESS_KEY_ID="${EDGLY_KEY}" \
  65. AWS_SECRET_ACCESS_KEY="${EDGLY_SECRET}" \
  66. aws s3 ls \
  67. --region="us-east-1" \
  68. "s3://crates.edgly.net/756b8efaed084669b02cb99d4540d81f/default/releases/uppy/v${version}/package.json" > /dev/null 2>&1 && fatal "Tag ${version} already exists"
  69. echo "✅"
  70. echo "--> Obtain relevant npm files for uppy ${version} ... "
  71. if [ -z "${remoteVersion}" ]; then
  72. npm pack || fatal "Unable to fetch "
  73. else
  74. npm pack "uppy@${remoteVersion}"
  75. fi
  76. echo "✅"
  77. rm -rf /tmp/uppy-to-edgly
  78. mkdir -p /tmp/uppy-to-edgly
  79. cp -af "uppy-${version}.tgz" /tmp/uppy-to-edgly/
  80. tar zxvf "uppy-${version}.tgz" -C /tmp/uppy-to-edgly/
  81. echo "--> Upload to edgly.net CDN"
  82. pushd /tmp/uppy-to-edgly/package
  83. # --delete \
  84. env \
  85. AWS_ACCESS_KEY_ID="${EDGLY_KEY}" \
  86. AWS_SECRET_ACCESS_KEY="${EDGLY_SECRET}" \
  87. aws s3 sync \
  88. --region="us-east-1" \
  89. --exclude 'website/*' \
  90. --exclude 'node_modules/*' \
  91. --exclude 'examples/*/node_modules/*' \
  92. ./ "s3://crates.edgly.net/756b8efaed084669b02cb99d4540d81f/default/releases/uppy/v${version}"
  93. echo "Saved https://transloadit.edgly.net/releases/uppy/v${version}/"
  94. popd > /dev/null 2>&1
  95. rm -rf /tmp/uppy-to-edgly
  96. echo "${version}" | env \
  97. AWS_ACCESS_KEY_ID="${EDGLY_KEY}" \
  98. AWS_SECRET_ACCESS_KEY="${EDGLY_SECRET}" \
  99. aws s3 cp \
  100. --region="us-east-1" \
  101. --content-type="text/plain" \
  102. - "s3://crates.edgly.net/756b8efaed084669b02cb99d4540d81f/default/releases/uppy/latest.txt"
  103. echo "Saved https://transloadit.edgly.net/releases/uppy/latest.txt"
  104. echo "${version}" | env \
  105. AWS_ACCESS_KEY_ID="${EDGLY_KEY}" \
  106. AWS_SECRET_ACCESS_KEY="${EDGLY_SECRET}" \
  107. aws s3 cp \
  108. --region="us-east-1" \
  109. --content-type="text/plain" \
  110. - "s3://crates.edgly.net/756b8efaed084669b02cb99d4540d81f/default/releases/uppy/v${majorVersion}-latest.txt"
  111. echo "Saved https://transloadit.edgly.net/releases/uppy/v${majorVersion}-latest.txt"
  112. popd > /dev/null 2>&1