Deploy rails on Appengine


Configuring your App with app.yaml

bookmark_border

Python |Java |Node.js |Go |Ruby |PHP |.NET

A Ruby app in App Engine is configured using an app.yaml file, that contains CPU, memory, network and disk resources, scaling, and other general settings including environment variables.

About app.yaml files

You can specify the runtime configuration for your Ruby app, including versions and URLs, in the app.yaml file. This file acts as a deployment descriptor of a specific service version. You must first create the app.yaml file for the default service of your app before you can create and deploy app.yaml files for additional services.

appengine/hello_world/app.yaml

View on GitHub

runtime: ruby

env: flex

entrypoint: bundle exec ruby app.rb


# This sample incurs costs to run on the App Engine flexible environment.

# The settings below are to reduce costs during testing and are not appropriate

# for production use. For more information, see:

# https://cloud.google.com/appengine/docs/flexible/ruby/configuring-your-app-with-app-yaml

manual_scaling:

instances: 1

resources:

cpu: 1

memory_gb: 0.5

disk_size_gb: 10

To learn more about structuring multiple services and app.yaml files in your app, see Structuring Web Services. Depending on the complexity of your app's service, you might only need to define a few elements in the corresponding app.yaml file. The following example demonstrates what a simple Ruby app might require in the flexible environment:

You can specify a unique name for your app.yaml files, but then you must specify the file name with the deployment command as well. For example, if you name your app.yaml file service-name-app.yaml or app.flexible.yaml, then you must deploy your app using either:

gcloud app deploy service-name-app.yaml

gcloud app deploy app.flexible.yaml

All configuration elements

For a complete list of all the supported elements in this configuration file, see the app.yaml reference.


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 command line tool 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:

appengine/rails-hello_world/app.yaml

View on GitHub

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.

  2. bundle exec rails secret

  3. Copy the generated secret key. You use the secret key in the next step.

  4. 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.
    appengine/rails-hello_world/app.yaml
    View on GitHub

  5. entrypoint: bundle exec rackup --port $PORT

  6. env: flex

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

  2. gcloud app create

  3. Select a region that supports 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.

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 Viewer located in the Cloud Console, or by using gcloud app logs read. You can learn more about reading logs by using the Cloud SDK.