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 (the name above has already been taken and will not work for you, sorry!). 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 "cleanup" 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.

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

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.

Run the following command in the cloud shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

In this codelab you will be using Runtime Configuration Service to store secret information and Cloud Container Builder used to read secret information from Runtime Configuration at deployment later in this codelab.

gcloud services enable runtimeconfig.googleapis.com
gcloud services enable cloudbuild.googleapis.com

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

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 save and close the file in nano by using (Ctrl + x), which will prompt:

Press the 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:

You can test the updated Rails application using the Web Preview shown in the previous step and you should see the following:

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.

To store the secret key, you will use the Runtime Configuration Service.

Create a new configuration named flex-env-config by using the following command:

gcloud beta runtime-config configs create flex-env-config

Next, add a variable for the SECRET_KEY_BASE environment variable:

gcloud beta runtime-config configs variables set \
  --config-name=flex-env-config --is-text \
  SECRET_KEY_BASE "[SECRET_KEY]"

Replace [SECRET_KEY] with the value generated from running bundle exec rails secret. 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.

Next, 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:

# How to start your application
entrypoint: bundle exec rackup --port $PORT
# Use App Engine flexible environment
env: flex
# Use the supported Ruby runtime 
runtime: ruby

# App Engine flexible environment will load the configuration
# values from the Runtime Configuration Service defined by 
# flex-env-config and create a .env file used by your
# application to determine environment values.
runtime_config:
  dotenv_config: flex-env-config

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:

Next, add the dotenv-rails gem to your Gemfile by using the following command:

bundle add dotenv-rails

Bundler will add the gem to your Gemfile and run bundle install to install the gem.

Finally, App Engine requires permission to load configurations from Runtime Configuration Service at build time.

Retrieve the project number by listing available projects by using the following command:

export PROJECT_ID=$(gcloud config get-value project)
gcloud projects list --filter $PROJECT_ID

Copy your projects PROJECT NUMBER for the project you will use to deploy your app.

Use the PROJECT_NUMBER in the following command to add the a new member to your project IAM policy for the role roles/editor to run database migrations:

gcloud projects add-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:[PROJECT_NUMBER]@cloudbuild.gserviceaccount.com \
--role=roles/editor

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 --region us-central

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.