Build a blog application on Google App Engine: Deploy to Google Cloud (part 8)

Sébastien Loix
Google Cloud - Community
4 min readDec 14, 2018

This is the eight and last part of a multi-part tutorial on how to build a small blog application in Node.js using the Google Datastore and deploy it to Google App Engine. If you missed the beginning, jump to the first part where I explain how to set up the project and where you’ll find the links to the other parts of the tutorial.

Our small blog is done and it is looking awesome! In this post, we are going to deploy the application to Google App Engine. For that, we will use the gcloud CLI tool from Google that makes this process extremely easy. In case you haven’t installed it yet on your machine, go to the download page and do that first.

In order to deploy our app, gcloud needs a descriptor file. By default, it is an app.yaml file located at the root of the project. This is where we will configure our application, set the Node.js runtime version, allocate resources, scaling rules, declare environment variables, etc… You can find here the documentation for all the options available.

Create Datastore Indexes

Up until now, we have been running our application against the local Datastore emulator which does not need indexes to be declared in order to execute queries. The Cloud Datastore does require that we configure indexes because it needs to know in advance which queries the application will make. For more information about indexes and why we need to configure them, read the documentation.

Meanwhile we were building the application, each time we executed a query against the local Datastore emulator, the indexes needed to be configured were automatically declared for us in an index.yamlfile inside the emulatorWEB-INF folder. If you launched the emulator datastore from the project npm script (npm run local-datastore), you should see this file in the <project-folder>/local-datastore/WEB-INF folder. For the sake of simplicity, and in case your local emulator folder was in a different location, the content of this file has been copied to the “index.yaml” file at the root of the project.

Execute the following command to configure the indexes on the live Google Cloud Datastore:

gcloud datastore create-indexes ./index.yaml

Great! we are now ready to deploy our application to Google App Engine.

gcloud to deploy the application

Here are some examples of how we could deploy an application with gcloud.

# This is as simple as it gets. gcloud will look for an "app.yaml" 
# at the root of the project, deploy the application and redirect
# all the traffic to it. It will also give the application a random
# "version" number.
gcloud app deploy
# Here again, gcloud will look for an "app.yaml", deploy the
# application, but it will not redirect the traffic (no-promote) to
# it. We also manually set the version to "1-0-0"
gcloud app deploy -v 1-0-0 --no-promote
# To specify a custom descriptor file, just pass it as last argument
gcloud app deploy -v dev --no-promote app.dev.yaml

In our case, we won’t use gcloud directly but a npm script instead. This allows us to hook other npm scripts (to build the application before deploying it), giving us more flexibility in the deployment process.

Deploy the application

Deploying our application and make it available to our users will be done in 2 steps. First, we will deploy to a unique application version following the Semantic Versioning. If you haven’t heard about semantic versioning read about it here. We won’t make the deploy visible to our users at that stage. This will allow us to first test the application running on Google Cloud and make sure everything runs correctly.

Once we have fully tested the application, the second step will be to promote the application and send all the traffic to it. Let’s get started!

1. Deploy the application

npm run deploy -- -v 1-0-0
# or
yarn deploy -v 1-0-0

As App engine does not allow dot “.” inside the application version we use “-” dash instead. You will see that the application code (server and client) will be built first and then a prompt will appear from gcloud to confirm the deployment. Once the application is deployed you will have an URL to test the application that will look something like:

https://1-0-0-dot-<your-google-appengine-project>.appspot.com

You can now test the application and make sure it is working properly. If you find any issue, correct them and re-run the above command with the same version. This will override the version in App Engine.
Once you have fully tested the application and you are happy with it, you can promote it. Promoting a version will send all the traffic to it and make it available to your users.

2. Promote the application

npm run promote -- -v 1-0-0
# or
yarn promote -v 1-0-0

Great! You should have now a fully working blog application running on Google App Engine!

For a real-world application, there are still a few things that we’d need to polish. For example, we would need to secure the Admin routes behind an authentication system, and we would probably add a custom domain. I leave that as an exercise for you to do.

And with that, we have completed the tutorial. I hope you’ve enjoyed it as much as I did writing it and that you realize by now how easy it is to launch your next Node.js project on Google Cloud.

Like always, let me know in the comments if you found any issue or if something is not clear.

Happy coding!

--

--