Deploying a full-stack dApp to Google Cloud
In the previous tutorials in this series, we saw how to develop a full-stack ethereum-based blockchain dApp.
In this tutorial, we learn how to deploy the dApp to two of Google's Cloud offerings, Firebase, and Cloud storage.
Google Cloud is a suite of products similar to Amazon's AWS, ranging from computation and databases to storage.
Google Firebase is an abstraction and subset of Google Cloud, making it easier for developers to use Google Cloud infrastructure. For example, Firebase Functions is an easier interface to Cloud Functions. Similarly, Firebase Storage and Hosting is an abstraction built on top of Cloud Storage.
In this tutorial, we cover deploying a dApp to both Firebase and/or Google Cloud Storage.
If you are looking for fully-featured static hosting, then Firebase is the recommended option. Benefits include:
- Free https certificate for your domain
- Free hosting and subdomain from Google (both a
yourproject.web.app
andyourproject.firebaseapp.com
domains) - No complicated setup with CNAME records and bucket setups (e.g. setting up permissions, etc.)
- An easy to use rollback/release history
- No billing setup needed
If you would rather have a barebones static hosting solution, then Google Cloud Storage may be more suited. However, you need to have a domain name ready, be OK with changing some domain records and need to set up your own https certificates (if needed).
For more information on the Google Cloud products and options, see the official Serving Websites documentation.
Prerequisites
We assume that you have followed and completed the previous tutorials in this series, and now have:
- A truffle project setup and working,
- Your truffle project contains smart contract code,
- Your truffle project has an existing frontend (in React),
- You have deployed your smart contract code to Rinkeby using Infura, and
- Your React frontend (on localhost) is communicating with your smart contracts on Rinkeby.
Firebase
Sign up
- Sign up for Firebase using your Google account.
- Follow the instructions to create a new Firebase project.
- When prompted on the Project Overview page, select Web to create a new web project.
- Name your web app and check the box for Also setup Firebase Hosting for this app.
- You don't need to add the Firebase SDK, so skip the step if asked.
Local setup
-
In terminal, install the Firebase CLI:
shell npm install -g firebase-tools
-
In the same terminal, sign in to Firebase:
shell firebase login
-
Still in terminal, navigate to the fullstack dApp root directory using the
cd
command (e.g.cd truffle-react-box-frontend/
), and initiate the project withfirebase init
.- When prompted, select Hosting, press space bar to select it, then enter to go to the next screen.
- When prompted, select Use an existing project, then select the project you created on the Firebase website.
- When asked What do you want to use as your public directory?, press enter. We will change this manually later.
- When asked Configure as a single-page app (rewrite all URLs to /index.html)?, select y.
Making deployment easy
- In your editor, open the newly created
firebase.json
file.- Replace
json "public": "public",
with:json "public": "client/build", "predeploy": [ "cd client && npm run build" ],
This change tells Firebase to build your React dApp frontend, then use those build files to deploy to Firebase Hosting.
- Replace
-
You are now ready to deploy. From your terminal (still in the fullstack dApp root directory), enter:
shell firebase deploy
Accessing your dApp
- Congratulations, your dApp is now live, hosted on Google's scalable infrastructure thanks to Firebase. You can access your dApp by going to the provided
Hosting URL
shown in the terminal. - Whenever you update your React frontend code, repeat step 2 in Making deployment easy to update your live dApp frontend code.
Google Cloud Storage
Initial setup
- Select or create a GCP project on the project selector page
- If you are creating a new project, select the Create in the top right, name your project, and add it to an organization if you would like to keep things organized.
- Since we are utilizing Cloud Storage, you need to enable billing on your account. You will only be charged for costs above the generous free tier.
- You can learn how to enable billing for the project here.
- If you already have a billing account added to GCP, then you can choose this billing account from the manage billing accounts page
- You need to have a domain you own. In this tutorial, we use the domain "example.com" as a placeholder.
- To verify that you own the domain and can use it with Cloud Storage, you need to follow the steps to verify that you own or manage the domain.
- If you purchased your domain name via Google Domains, then verification is automatic.
Create a CNAME record
-
Once you have completed the above, you need to add a CNAME record to your domain. Consult the support documentation of your domain provider to find out how to add one.
-
Create a
CNAME
record that points to "c.storage.googleapis.com"text NAME TYPE DATA www.example.com CNAME c.storage.googleapis.com
-
Create a Google Storage bucket
- Go to the Cloud Storage browser and create a bucket whose name matches the CNAME you created.
- Enter the bucket information and choose the default settings for each step.
Install the gsutil
tool
The gsutil
utility helps you use the Google Cloud platform from the terminal. Follow the instructions to Install the Cloud SDK. When prompted, choose the project that you created initially.
Upload the dApp's static files
- From the terminal, navigate to the fullstack dApp root directory using the
cd
command (e.g.cd truffle-react-box-frontend/
). - As you would for any React.js based app, create an optimized production build of your frontend:
- Navigate to the
client/
directory and run the command:npm run build
- Navigate to the
-
In the same
client/
directory, run the command:shell gsutil rsync -R build gs://www.example.com
- This uploads all the contents of your
client/build/
directory to the Storage bucket you created
- This uploads all the contents of your
Make all files in your bucket publicly accessible
- Since we are only hosting a static website in the bucket, we want all the files to be publicly readable.
- In the Cloud Storage browser, select the dApp's website bucket you created.
- Select the Permissions tab.
- Select Enable in the Simplify access control with Bucket Policy Only alert.
- Confirm the selection.
- Select Add members and enter "allUsers" in the New members field, with the "Storage Object Viewer" role.
- Your bucket is now publicly accessible.
- If you set up the CNAME record properly and has propagated, your domain name points to the bucket's contents.
- Congratulations, your dApp is now live, hosted on Google Cloud Storage!
- Whenever you update your React code, follow the steps in Deploying your dApp to update your deployed dApp.
- Kauri original title: Deploying a full-stack dApp to Google Cloud
- Kauri original link: https://kauri.io/deploying-a-fullstack-dapp-to-google-cloud/2ee06d313ffa41348f916afa13a78bbd/a
- Kauri original author: David T (@david-t)
- Kauri original Publication date: 2019-10-03
- Kauri original tags: dapp, google-cloud, firebase, deploy
- Kauri original hash: QmfKHpAtBoPdVe6QWu48Pz1eVEHiAc36vqvPybXS892aSY
- Kauri original checkpoint: QmUP9qZg9vxiTYmDyCRfVzpyYLQbtd6r3GAM7CyqCFhShv