DEV Community

Emmanuel
Emmanuel

Posted on

Deploying Adonis.js 5.0 to Google App Engine (Part 1)

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 logo Obapelumi / adonisjs-5-blog

Simple Adonis.js 5.0 blog hosted on Google Cloud Platform

Node.js Blog Hosted On Google Cloud Platform

Simple Adonis.js blog hosted on Google Cloud Platform

Setup

Install the adonis cli tool and run npm install or yarn install.

Once the installation is complete you will need to complete the following steps:

  1. Create a MySQL database: This application uses MySQL for data persistence. You will need to create a MySQL database and take note of the database name and other connection parameters.
  2. Generate your application key: Run node ace generate:key. This key is used internally by the application for encryption purposes.
  3. Create a .env file based off .env.example file and populate its contents accordingly. Environment Variables
  4. Create your database tables by running migrations: In the terminal run node ace migration:run --force

You can now run npm run dev or yarn dev to start the local development




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. 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.

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.

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?/.....
Enter fullscreen mode Exit fullscreen mode

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.

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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"
  },
  ...
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Oldest comments (3)

Collapse
 
medievalcodercom profile image
Ryan Ceasar Borromeo

I am following your guide but it seems the instruction is not complete.

Collapse
 
tngeene profile image
Ted Ngeene

Hey, can you complete this series?

Collapse
 
obapelumi profile image
Emmanuel

Hi, the second part is here: dev.to/obapelumi/deploying-adonis-.... Please let me know if it is helpful. Thanks a lot