Deploying Adonis.js 5.0 to Google App Engine (Part 1)
Published: 2020-04-29 19:21:53

---
title: Deploying Adonis.js 5.0 to Google App Engine (Part 1)
published: true
description: 
tags: 
---
Adonis.js is a powerful Node.js framework focused around developer ergonomics and stability. Version 5.0 promises a lot of exciting improvements and since it is currently in preview, I decided to have a go at it. My biggest curiosity was how to deploy to production. I had been exploring Google Cloud Platform's App Engine and decided to try it out with Adonis.js 5.0. In this article, I will walk through my entire experience, from building out the Adonis.js 5.0 application locally to deploying and setting up a  CI/CD pipeline on Google Cloud Platform. The deployment steps discussed in this article will likely apply to most Node.js applications so you should still find it helpful even if you do not use in Adonis.js. Let's get right to it.

### Our Adonis.js Application
The application we'll be hosting is an API which supports CREATE, READ UPDATE and DELETE of blog posts. I have already written the code to make our blog work. You can clone the repository and follow the setup instructions: {% github Obapelumi/adonisjs-5-blog %}

### Setting Up Our Google Cloud Project
To deploy our application, we need to create a project on Google Cloud Platform or select an existing one on the [Project Selector Page](https://console.cloud.google.com/projectselector2/home/dashboard?_ga=2.241558535.482020037.1586799515-1848780715.1586799515). We will also need to enable billing but don't worry Google Cloud offers a generous free-tier which we will not exhaust.

### Deploying our Application To Google's App Engine
Now that we have a project set up on Google Cloud we need to push code to Google's App Engine and give it instructions on how to run our app. To do this we need the Google Cloud SDK which gives us access to some Command Line Tools which we need for our deployment. You can find the installer for Windows, macOS or Linux [here](https://cloud.google.com/sdk/docs/downloads-interactive).

Once the Cloud SDK has been successfully installed we can use it to connect to our Google Cloud Project. To do this let's open up our command-line and `cd` into our Adonis.js 5.0 application's directory and then run `gcloud init` and follow the command-line prompts.

``` bash
C:\Users\Your Adonis.js Project Directory>gcloud init
Welcome! This command will take you through the configuration of gcloud.

Pick configuration to use:
 [1] Create a new configuration
Please enter your numeric choice:  1

Enter configuration name. Names start with a lower case letter and 
contain only lower case letters a-z, digits 0-9, and hyphens '-':  adonis-js-5
Your current configuration has been set to: [adonis-js-5]

Choose the account you would like to use to perform operations for 
this configuration:
 [1] Log in with a new account
Please enter your numeric choice:  1

Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?/.....
```
The initialization steps will open up your browser and require you to authorize the Google Cloud SDK. Once this is done you can return to your command-line to complete the initialization.

``` bash
You are logged in as: [<google-account>].

Pick cloud project to use:
 [1] voltaic-quest-274117
Please enter numeric choice or text value (must exactly match list
item):  1

Your current project has been set to: [voltaic-quest-274117].

.....
extra set up info from the google cloud SDK..
....

C:\Users\Your Adonis.js Project Directory>
```
Next, we need to tell Google App Engine to run our application using **Node.js 10**, specify environment variables and a start-up command for our application. To do this first we create an `app.yaml` file in the root of our application with the following content:

```
runtime:  nodejs10

env_variables: 
  PORT: <The Port Your Application Listens On>
  HOST: <mostly 127.0.0.1>
  NODE_ENV: <production or development>
  APP_KEY: <Your Application Key>
  DB_CONNECTION: <MySQL, Postgres or SQLite>
  DB_HOST: <Database Host IP Address>
  DB_USER: <Database User>
  DB_PASSWORD: <Database Password>
  DB_NAME: <Database Name>
```

Adonis.js 5.0 has a build step which compiles the TypeScript code written into JavaScript in the `build` directory. Therefore, the entry point for our application will be `build/server.js` By default, the App Engine will look for the `start` script in our `package.json` file so we can specify our start-up command there. Here's how that would look:
```
{
  "name": "blog",
  "scripts": {
    "build": "node ace build",
    "dev": "node ace serve --watch",
    "start": "node build/server.js"
  },
  ...
}
```

Now we can run the following commands to compile and deploy our application: 
```
node ace build \\ Adonis.js 5.0 build step
gcloud app deploy
``` 
If we did everything right we can visit `https://<our-project-id>.appspot.com` to see the application running.

In the next article, we will set up version control for our application and see how we can up Google Cloud to deploy our application once we push to our GitHub repository's master branch.