Skip to content

Instantly share code, notes, and snippets.

@ashgkwd
Last active November 9, 2022 15:56
Show Gist options
  • Save ashgkwd/a3c412e4f0c0c38c352683c7f25bce37 to your computer and use it in GitHub Desktop.
Save ashgkwd/a3c412e4f0c0c38c352683c7f25bce37 to your computer and use it in GitHub Desktop.
Deploy to GCS bucket for static website hosting
DOMAIN_NAME="my.web.site";
PROJECT_ID="my-gcp-project-id";
# create bucket of domain name
gsutil mb -p $PROJECT_ID -b on gs://$DOMAIN_NAME
# set index file
gsutil web set -m index.html gs://$DOMAIN_NAME
# set error file
gsutil web set -e 404.html gs://$DOMAIN_NAME
# permissions: let the world see our files ๐Ÿ‘€
gsutil iam ch allUsers:legacyObjectReader gs://$DOMAIN_NAME
# deploy files to bucket
gsutil -m rsync -d -r local/path/of/site gs://$DOMAIN_NAME
@ashgkwd
Copy link
Author

ashgkwd commented Oct 7, 2021

You can refer to this great video to understand DNS and other configs needed for GCS bucket websites to work: https://www.youtube.com/watch?v=sUr4GBzEqNs

@ashgkwd
Copy link
Author

ashgkwd commented Oct 7, 2021

In the case of React and other websites which need to load index.html on any non-available file path, you can set 404 to respond with index.html. That way if the user reloads on some URL on your website, it will get index.html. This is especially helpful when routes are handled inside the application. The rest of the static files (images, CSS, js etc) will continue to be served as is.

gsutil web set -e index.html gs://$DOMAIN_NAME

@sidpalas
Copy link

sidpalas commented Oct 7, 2021

Hey @ashgkwd!

YouTube blocks comments with URLs in them, but if you want to comment again that you created this gist, I can add a link in a reply!

Also, FYI i have the code in the repo for my channel here as well: https://github.com/sidpalas/devops-directive/blob/master/2020-09-28-gcs-cloudflare-static-site/create-bucket.sh

@ashgkwd
Copy link
Author

ashgkwd commented Oct 7, 2021

Hey @sidpalas ๐Ÿ‘‹, It will be nice if you comment the link. I did not know that YouTube blocks links ๐Ÿ™ˆ. Thanks

@sidpalas
Copy link

sidpalas commented Oct 7, 2021

๐Ÿ‘ -- if you add your comment back on youtube (without link) ill reply with the link ๐Ÿ˜€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment