Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -o errexit
  4. set -o nounset
  5. set -o pipefail
  6. set -o noglob
  7.  
  8. # This script will deploy studio to the given cloudfront distribution/s3 bucket.
  9. # This should only be called from the continuous deployment pipeline
  10. # Usage: bin/s3-deploy
  11.  
  12. sync_s3() {
  13. bucket_name=${1}
  14. tag_name=${2}
  15.  
  16. echo -e "Syncing assets..."
  17. aws s3 sync ./dist s3://${bucket_name}/${tag_name} --delete
  18. echo -e "${green}Done${reset}"
  19. }
  20.  
  21. change_origin_path() {
  22. tag_name=${1}
  23. cloudfront_distribution_id=${2}
  24.  
  25. echo -e "Changing cloudfront origin..."
  26. current_distribution_config=$(aws cloudfront get-distribution --id ${cloudfront_distribution_id})
  27. next_distribution_config=$(bin/lib/update-distribution ${tag_name} "${current_distribution_config}")
  28. etag=$(bin/lib/get-etag "${current_distribution_config}")
  29. aws cloudfront update-distribution --id ${cloudfront_distribution_id} --distribution-config ${next_distribution_config} --if-match ${etag}
  30. echo -e "${green}Done${reset}"
  31. }
  32.  
  33. invalidate_cache() {
  34. cloudfront_distribution_id=${1}
  35.  
  36. echo -e "Invalidating cloudfront cache..."
  37. aws cloudfront create-invalidation --distribution-id ${cloudfront_distribution_id} --paths /*
  38. echo -e "${green}Done${reset}"
  39. }
  40.  
  41. deploy_to_git_tag() {
  42. tag_name=${1}
  43. cloudfront_distribution_id=${2}
  44. bucket_name=${3}
  45.  
  46. echo -e "Deploying ${blue}${1}${reset}"
  47. sync_s3 ${bucket_name} ${tag_name}
  48. change_origin_path ${tag_name} ${cloudfront_distribution_id}
  49. invalidate_cache ${cloudfront_distribution_id}
  50. }
  51.  
  52. main() {
  53. source bin/lib/colors
  54. cloudfront_distribution_id=${CLOUDFRONT_DISTRIBUTION_ID}
  55. bucket_name=${BUCKET_NAME}
  56. deploy_tag=$(git describe)
  57.  
  58. if [ "${deploy_tag}" ]; then
  59. deploy_to_git_tag ${deploy_tag} ${cloudfront_distribution_id} ${bucket_name}
  60. echo -e "${green}Deploy success${reset}"
  61. else
  62. echo -e "${red}Deploy failure: no tag${reset}"
  63. fi
  64. }
  65.  
  66. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement