Auto deploy a Node app to the Google App Engine
I have built a Shopify admin application using Node.js and Next.js and I have been looking at the best places to host it. I originally used Heruko which is super simple and requires barely any setup. However, the free tier is not very reliable and I didn’t want to pay for upgraded plans before I know if the application will be successful. After doing some research, I found out that Google App Engine offers a really good free option as long as you stay within the limits. I’ve got experience with AWS and I have always wanted to give Google cloud a try so here is what I learnt.
Firstly, you’ll need to sign up here, you can see that they offer $300 credit for the first year if you have an application that needs to use more resources. Then once you’ve signed up, you’ll need to create a new application. The free tier is only available for the standard environment, so make sure to select that.
Google cloud does have an SDK that allows you to deploy via the command line. This is what I used to start with but I’m used to having everything automated so I didn’t really want to have to manually run a command every time. So this tutorial will show you how to link to a repository and trigger builds after a push to the master branch (or any other branch).
There are a few files that you need to setup within your main project folder.
- app.yaml
For a simple setup, you won’t need much in this file. The only thing that you must add is the runtime, which is responsible for installing your code & dependencies. You can also add environment variables here, but I should stress that these shouldn’t contain sensitive data because it will be committed into your repository. By default your NODE_ENV will be set to production. Here is the link to the documentation and here is a bare minimum setup file
2. cloudbuild.yaml
This defines the build configuration. If you’ve ever used bitbucket pipelines, it’s pretty similar to that. You add the name of the cloud builder image and the steps. Full documentation is here. For my deployment, I’ve chosen to use the gcloud builder, because this is consistent with using the sdk command on my computer. So when I push to my master branch it will run gcloud app deploy automatically, which will install all of the npm dependencies too.
3. package.json gcp-build step
As I’m running a next.js application, I need to run next build for my application to work. You can add custom build steps within package.json. Below shows the scripts that I have in my package.json with the gcp-build step
Next we need to link to your repository. I had an existing repository already on Bitbucket, however you can choose to use a Google repository. While you’re logged into the Google Cloud Platform, make sure that you are on the correct project and in the left hand menu, scroll down until you see Source Repositories under the tools heading.
You can then choose an empty repository or to link to an existing one from Bitbucket or Github. Once you’ve selected your project and linked to an existing repository or created a new one, you then need to setup a build trigger. You need to click on the settings for the repository which will take you to this page
Click on the cloud build triggers link and click on create trigger. Choose the branch that you would like the build to trigger on and point the configuration file to the cloudbuild.yaml file created above and save.
OK so now we’re almost there. If you were to push to the master branch (or whichever branch you selected) then a build would run. However, this will fail at the moment because we need to enable the app engine API here. Then you need to go to Cloud Builds settings and enable App Engine Admin. Now it’s all ready! Go ahead and push/merge to your master branch.
You will be able to see all of the build logs within the Cloud Builds page and you can find your deployed app link on the App Engine Services page
That’s it! definitely not as simple as Heroku but there are so many extra options available within Google App Engine that I’m still yet to explore.