Run Rails 7 on App Engine flexible environment

Get started developing Ruby on Rails apps that run on App Engine flexible environment. The apps you create run on the same infrastructure that powers all of Google's products, so you can be confident that they can scale to serve all of your users, whether there are a few or millions of them.

This tutorial assumes you are familiar with Rails web development. It walks you through deploying a new Rails app.

This tutorial supports and requires Ruby 3.0 or later.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Install the Google Cloud CLI.
  7. To initialize the gcloud CLI, run the following command:

    gcloud init

Set up your local environment for Rails

To set up your local environment for Rails development, you'll need to:

  1. Install Ruby version 3.0 or later.

  2. Install the Rails 7 gem.

  3. Install the Bundler gem.

Alternatively, you can use Cloud Shell, which comes with Ruby, Rails, and the Google Cloud CLI already installed.

For additional information on installing Rails and its dependencies, see the official Getting started with Rails guide.

After you complete the prerequisites, you can create and deploy a Rails app. The following sections guide you through configuring, running, and deploying an app.

Create a new app

  1. Create a new Rails sample app.

    rails new appengine_example
    
  2. Go to the directory that contains the sample code.

    cd appengine_example
    

Run the app locally

To run the new Rails app on your local computer:

  1. Start a local web server.

     bundle exec rails server
    
  2. In your browser, go to http://localhost:3000/.

    The sample app displays the Rails logo with the Rails and Ruby versions.

Add a simple page

Now add a Welcome page to the generated Rails app.

  1. To generate the scaffolding for a new page, create a new Rails controller named WelcomeController with an index action.

    bundle exec rails generate controller Welcome index
    
  2. Open the file app/views/welcome/index.html.erb to see the following boilerplate HTML.

    <h1>Welcome#index</h1>
    <p>Find me in app/views/welcome/index.html.erb</p>
  3. Modify the file however you like. For example, you can use the following content:

    <h1>Welcome</h1>
    <p>This is a home page for a new Rails App on Google Cloud Platform!</p>
  4. Set the index controller action as the root action for Rails. Then, whenever a user visits the Rails app, they see your welcome page.

  5. Open the file config/routes.rb to see the following generated content.

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

    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
  7. Save the file and close it. Test the Rails app as you did before.

Deploy the app to App Engine flexible environment

App Engine flexible environment uses a file called app.yaml. to describe an app's deployment configuration. If this file isn't present, the gcloud CLI tries to guess the deployment configuration. However, it is a good idea to provide this file because Rails requires a secret key in a production environment.

To configure the sample app for deployment to App Engine, create a new file named app.yaml at the root of your sample app directory and add the following:

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

Configure the Rails secret key

When you deploy a Rails app in the production environment, set the environment variable SECRET_KEY_BASE to a secret key that is used to protect user session data. This environment variable is read in the config/secrets.yml file.

  1. Generate a new secret key.

    bundle exec rails secret
    
  2. Copy the generated secret key. You use the secret key in the next step.

  3. Open the file app.yaml that you created earlier, and add an env_variables section. The env_variables sets environment variables in the production environment in App Engine flexible environment. Your app.yaml should look like the example below, with [SECRET_KEY] replaced with your copied secret key.

    entrypoint: bundle exec rackup --port $PORT
    env: flex
    runtime: ruby
    
    env_variables:
      SECRET_KEY_BASE: [SECRET_KEY]

Set up an App Engine flexible environment app

If this is the first time you are deploying an app, you need to create an App Engine flexible environment app to help you select the region in which to run the Rails app. You can read more about regions and zones.

  1. Create an App Engine app. By default, the following command creates two instances:

    gcloud app create
    
  2. Select a region that supports the App Engine flexible environment for Ruby apps.

Deploy to App Engine flexible environment

Deploy the sample app by running the following command:

gcloud app deploy

Wait for the message that notifies you that the update has completed. This can take several minutes.

Access the deployed Rails app

  1. To retrieve your project ID, run gcloud info.

  2. In your browser, enter the following URL:

    https://PROJECT_ID.REGION_ID.r.appspot.com

    Replace the following:

The following content is displayed.

Screenshot of new Rails app running

This time, your request is served by the Rails app running in App Engine flexible environment.

This command deploys the app as described in app.yaml and sets the newly deployed version as the default version, causing it to serve all new traffic. As the app deploys, you might see several repeated messages while the platform checks whether the app is serving. This is normal. Wait for the message that notifies you that the update of the app is complete.

If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.

Read App Engine logs

Now that you have deployed your Rails app, you may want to read the logs. You can read the app logs by using the Logs Explorer located in the Google Cloud console, or by using gcloud app logs read. You can learn more about reading logs by using the gcloud CLI.

Clean up resources

After you finish the tutorial, you can clean up the resources that you created so that they stop using quota and incurring charges. The following sections describe how to delete or turn off these resources.

Delete project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete an App Engine version

To delete an app version:

  1. In the Google Cloud console, go to the Versions page for App Engine.

    Go to Versions

  2. Select the checkbox for the non-default app version that you want to delete.
  3. To delete the app version, click Delete.

What's next