In this codelab, you will learn how to deploy a new Ruby on Rails application or Rails app for short to Google App Engine Flexible environment. You will learn how to use Cloud Shell and the Cloud SDK to get started without downloading anything.

Google App Engine Flexible environment applications are easy to create, easy to maintain, and easy to scale as your traffic and data storage changes. With App Engine, there are no servers to maintain. You simply upload your application and it's ready to go.

App Engine applications automatically scale based on incoming traffic. load balancing, microservices, authorization, SQL and NoSQL databases, traffic splitting, logging, search, versioning, roll out and roll backs, and security scanning are all supported natively and are highly customizable.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with Ruby?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the Project ID, a unique name across all Google Cloud projects. It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "clean up" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

Start Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud. This Debian-based virtual machine is loaded with all the development tools you'll need (gcloud, ruby, gem, bundle and more), it offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. This means that all you will need for this codelab is a browser (yes, it works on a Chromebook).

To activate Google Cloud Shell, from the developer console simply click the button on the top right-hand side (it should only take a few moments to provision and connect to the environment):

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID:

gcloud auth list
Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project
[core]
project = <PROJECT_ID>

If for some reason the project is not set, simply issue the following command:

gcloud config set project <PROJECT_ID>

Looking for your PROJECT_ID? Check out what Project ID you used in the setup steps or look it up in the console dashboard :

In the Google Cloud Shell, you can install the Rails gem by using:

gem install rails

Now, you can verify the gem installation by using:

rails --version

You should see something similar to the following:

Rails 5.x.x

Summary

In this step, you installed the Rails gems and verified installation by checking the version.

Now that you have Rails installed, you can generate the scaffolding of a new Rails app by using:

rails new app_name

Move into the directory created by Rails for the application:

cd app_name

You can view the generated files for a new Rails applications by using:

ls

You should see something similar to:

Summary

In this step, you generated a new Rails app.

Now that we have a Rails app, let's test it using the Web Preview functionality provided by the Cloud Shell environment. By default the Web Preview uses port 8080.

Start the Rails app server listening on port 8080:

bundle exec rails server --port 8080

Once the application starts, click on the Web Preview icon in the Cloud Shell toolbar and choose Preview on port 8080.

A tab in your browser opens and connects to the server you just started. You should see the following image!

You can now stop the Rails server by using (Ctrl + c) in the Cloud Shell.

Summary

In this step, you tested your application your Rails app using the Web Preview on port 8080. You can read more about the Web Preview here.

Now you will add a Hello World page to the generated Rails application. You will generate the scaffolding for a new page by using rails generate. The following command will create a new Rails controller Welcome with an action called index.

bundle exec rails generate controller Welcome index

This command will generate the following new directories and files by default for the new controller and action.

Next, you will modify the index page using an editor of your choice vim, nano,or emacs. We will use the nano editor.

nano app/views/welcome/index.html.erb

The file should initially contain placeholder HTML such as this:

<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>

Modify the placeholder HTML to display your own welcome message. For example you could do the following:

<h1>Welcome</h1>
<p>This is a home page for a new Rails App on Google Cloud Platform!</p>

You can now close save and close the file in nano by using (Ctrl + x), which will prompt:

Type a letter y and then press the ENTER key one more time to confirm the filename for the following prompt:

Finally, you will set this new page as the root page for Rails, and whenever a user visits the Rails app they will see your Welcome#index page!

Open config/routes.rb using an editor of your choice vim, nano,or emacs. We will use nano:

nano config/routes.rb

Add the following line:

root 'welcome#index'

It should look similar to the following:

Rails.application.routes.draw do
  get 'welcome/index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  
  root 'welcome#index'
end

You can now close save and close the file in nano by using (Ctrl + x), which will prompt:

Type a letter y and then press the ENTER key one more time to confirm the filename for the following prompt:

Summary

In this step, you created a Welcome page for your new Rails app.

App Engine Flexible environment uses a file called app.yaml to describe an application's deployment configuration. If this file is not present, the gcloud tool will try to guess the deployment configuration. However, it is a good idea to provide this file because Rails requires a secret key in production.

A secret key is used to protect user session data. You can generate a secret key by using:

Bundle exec rails secret

Copy the secret key that's generated to your clipboard because you will use it in the next step.

Create a new file called app.yaml using vim, nano, or emacs. We will use nano:

nano app.yaml

And add the following in the file:

entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby

env_variables:
  SECRET_KEY_BASE: [SECRET KEY]

Replace [SECRET KEY] with the generated secret key.

You can now close save and close the file in nano by using (Ctrl + x), which will prompt:

Type a letter y and then press the ENTER key one more time to confirm the filename for the following prompt:

When the app is deployed, the environment variable SECRET_KEY_BASE in production will be set to the secret key. The environment variable SECRET_KEY_BASE is used in config/secrets.yml.

Summary

In this step, you generated a secret key to protect session data and created a deployment configuration app.yaml.

First, you need to create an App Engine instance by using:

gcloud app create

After this is enabled, you can deploy your app on App Engine by using:

gcloud app deploy

Check out your Rails App!

After the application is deployed, you can visit it by opening the URL

https://[PROJECT_ID].appspot.com in your web browser.

You should see your Welcome page! Here's an example:

Summary

In this step, you selected a Region for your App Engine instance and then deployed your Rails app.

You learned how to write and deploy your first App Engine Rails application!

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

Learn More

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.