1#!/usr/bin/env bash 2 3set -euo pipefail 4 5scriptdir=$(dirname "${BASH_SOURCE[0]}") 6declare -a buckets=("docs.expo.dev") 7target="${1-$scriptdir/out}" 8 9if [ ! -d "$target" ]; then 10 echo "target $target not found" 11 exit 1 12fi 13 14for bucket in ${buckets[@]}; do 15 16# To keep the previous website up and running, we deploy it using these steps. 17# 1. Sync Next.js static assets in \`_next/**\` folder 18# > Uploads the new generated JS and asset files (stored in hashed folders to avoid collision with older deployments) 19# 2. Sync assets in \`static/**\` folder 20# 3. Overwrite HTML dependents, not located in \`_next/**\` or \`static/**\` folder 21# > Force overwrite of all HTML files to make sure we use the latest one 22# 4. Sync assets and clean up outdated files from previous deployments 23# 5. Add custom redirects 24# 6. Copy domain-specific sitemap.xml separately since they're named separately in the build step 25 26echo "::group::[1/7] Sync Next.js static assets in \`_next/**\` folder" 27aws s3 sync \ 28 --no-progress \ 29 --exclude "*" \ 30 --include "_next/**" \ 31 --cache-control "public, max-age=31536000, immutable" \ 32 "$target" \ 33 "s3://${bucket}" 34echo "::endgroup::" 35 36echo "::group::[2/7] Sync assets in \`static/**\` folder" 37aws s3 sync \ 38 --no-progress \ 39 --exclude "*" \ 40 --include "static/**" \ 41 --cache-control "public, max-age=3600" \ 42 "$target" \ 43 "s3://${bucket}" 44echo "::endgroup::" 45 46# Due to a bug with `aws s3 sync` we need to copy everything first instead of syncing 47# see: https://github.com/aws/aws-cli/issues/3273#issuecomment-643436849 48echo "::group::[3/7] Overwrite HTML dependents, not located in \`_next/**\` or \`static/**\` folder" 49aws s3 cp \ 50 --no-progress \ 51 --recursive \ 52 --exclude "_next/**" \ 53 --exclude "static/**" \ 54 --exclude "*-sitemap.xml" \ 55 --exclude "*-robots.txt" \ 56 "$target" \ 57 "s3://${bucket}" 58echo "::endgroup::" 59 60echo "::group::[4/7] Sync assets and clean up outdated files from previous deployments" 61aws s3 sync \ 62 --no-progress \ 63 --delete \ 64 "$target" \ 65 "s3://${bucket}" 66echo "::endgroup::" 67 68declare -A redirects # associative array variable 69 70# usage: 71# redicts[requests/for/this/path]=are/redirected/to/this/one 72 73# Temporarily create a redirect for a page that Home links to 74redirects[versions/latest/introduction/installation.html]=versions/latest/introduction/installation/ 75# useful link on twitter 76redirects[versions/latest/guides/app-stores.html]=versions/latest/distribution/app-stores/ 77# Xdl caches 78redirects[versions/latest/guides/offline-support.html]=versions/latest/guides/offline-support/ 79# xdl convert comment 80redirects[versions/latest/sdk/index.html]=versions/latest/sdk/overview/ 81# upgrading expo -> upgrading sdk walkthrough 82redirects[versions/latest/workflow/upgrading-expo]=versions/latest/workflow/upgrading-expo-sdk-walkthrough/ 83# rename 84redirects[versions/latest/sdk/haptic/index.html]=versions/latest/sdk/haptics/ 85# duplicate docs file, consolidate into one page 86redirects[versions/latest/sdk/introduction/index.html]=versions/latest/sdk/overview/ 87# project-lifecycle is now covered by managed-vs-bare 88redirects[versions/latest/introduction/project-lifecycle/]=versions/latest/introduction/managed-vs-bare/ 89# exp-cli is now expo-cli 90redirects[versions/latest/guides/exp-cli.html]=versions/latest/workflow/expo-cli/ 91redirects[versions/latest/guides/exp-cli]=versions/latest/workflow/expo-cli/ 92# Migrated FAQ pages 93redirects[faq/image-background]=ui-programming/image-background/ 94redirects[faq/react-native-styling-buttons]=ui-programming/react-native-styling-buttons/ 95redirects[faq/react-native-version-mismatch]=troubleshooting/react-native-version-mismatch/ 96redirects[faq/clear-cache-windows]=troubleshooting/clear-cache-windows/ 97redirects[faq/clear-cache-macos-linux]=troubleshooting/clear-cache-macos-linux/ 98redirects[faq/application-has-not-been-registered]=troubleshooting/application-has-not-been-registered/ 99 100echo "::group::[5/7] Add custom redirects" 101for i in "${!redirects[@]}" # iterate over keys 102do 103 aws s3 cp \ 104 --no-progress \ 105 --metadata-directive REPLACE \ 106 --website-redirect "/${redirects[$i]}" \ 107 "$target/404.html" \ 108 "s3://${bucket}/${i}" 109 110 # Also add redirects for paths without `.html` or `/` 111 # S3 translates URLs with trailing slashes to `path/` -> `path/index.html` 112 if [[ $i != *".html" ]] && [[ $i != *"/" ]]; then 113 aws s3 cp \ 114 --no-progress \ 115 --metadata-directive REPLACE \ 116 --website-redirect "/${redirects[$i]}" \ 117 "$target/404.html" \ 118 "s3://${bucket}/${i}/index.html" 119 fi 120done 121echo "::endgroup::" 122 123echo "::group::[6/7] Copy over domain-specific sitemap.xml and robots.txt" 124aws s3 cp \ 125 --no-progress \ 126 "$target/${bucket}-sitemap.xml" \ 127 "s3://${bucket}/sitemap.xml" 128 129aws s3 cp \ 130 --no-progress \ 131 "$target/${bucket}-robots.txt" \ 132 "s3://${bucket}/robots.txt" 133echo "::endgroup::" 134 135echo "::group::[7/7] Notify Google of sitemap changes" 136if [ "$bucket" == "docs.expo.dev" ]; then 137 echo "Strings are equal" 138 curl -m 15 "https://www.google.com/ping\?sitemap\=https%3A%2F%2F${bucket}%2Fsitemap.xml" 139 echo "\n::endgroup::" 140else 141 echo "Skipping Gooogle sitemap ping for $bucket" 142fi 143done 144