Running Rails on the Cloud Run environment

Learn how to deploy a sample Rails application to Cloud Run and how to integrate managed databases, object storage, encrypted secrets, and build pipelines with serverless compute.

Deploying Rails applications involves integrating multiple services together to form a cohesive project. This tutorial assumes that you are familiar with Rails web development.

This tutorial requires Ruby 3.0 or later (Ruby 2.7 is also supported, see Understand the Code section) and Rails 6 or later.

Diagram showing the architecture of the deployment.
The Rails site is served from Cloud Run, which uses multiple backing services to store different data types (relational database information, media assets, configuration secrets, and container images). The backend services are updated by Cloud Build as part of a build and migrate task.

Objectives

  • Create and connect a Cloud SQL database to Active Record
  • Create and use Secret Manager to store and access a Rails master key securely
  • Host user-uploaded media and files on Cloud Storage from Active Storage
  • Use Cloud Build to automate build and database migrations
  • Deploy a Rails app to Cloud Run

Costs

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 Cloud project. Learn how to check if billing is enabled on a project.

  4. Enable the Cloud Run, Cloud SQL, Cloud Build, Secret Manager, and Compute Engine APIs.

    Enable the APIs

  5. Install and initialize the Google Cloud CLI.
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  8. Enable the Cloud Run, Cloud SQL, Cloud Build, Secret Manager, and Compute Engine APIs.

    Enable the APIs

  9. Install and initialize the Google Cloud CLI.
  10. Ensure sufficient permissions are available to the account used for this tutorial.

Preparing your environment

Set the default project

Set the default project configuration for the gcloud CLI by running the following command:

gcloud config set project PROJECT_ID

Replace PROJECT_ID with your newly created Google Cloud project ID

Cloning the Rails app

The code for the Rails sample app is in the GoogleCloudPlatform/ruby-docs-samples repository on GitHub.

  1. Clone the repository:

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
    
  2. Go to the directory that contains the sample code and run the following commands to make sure that the application is properly set up with the required gems and dependencies:

    Linux/macOS

    cd ruby-docs-samples/run/rails
    bundle install
    

    Windows

    cd ruby-docs-samples\run\rails
    bundle install
    

Preparing the backing services

This tutorial uses a number of Google Cloud services to provide the database, media storage, and secret storage that support the deployed Rails project. These services are deployed in a specific region. For efficiency between services, it is best that all services are deployed in the same region. For more information about the closest region to you, see Products available by region.

Set up a Cloud SQL for PostgreSQL instance

Rails supports multiple relational databases, including several offered by Cloud SQL. This tutorial uses PostgreSQL, an open source database commonly used by Rails apps.

The following sections describe the creation of a PostgreSQL instance, database, and database user for your Rails app.

Create a PostgreSQL instance

Console

  1. In the Cloud console, go to the Cloud SQL Instances page.

    Go to the Cloud SQL Instances page

  2. Click Create Instance.

  3. Click Choose PostgreSQL.

  4. In the Instance ID field, enter a name for the instance (INSTANCE_NAME).

  5. In the Password field, enter a password for the postgres user.

  6. Use the default values for the other fields.

  7. Click Create Instance.

gcloud

  • Create the PostgreSQL instance:

    gcloud sql instances create INSTANCE_NAME \
        --database-version POSTGRES_12 \
        --tier db-f1-micro \
        --region REGION
    

    Replace the following:

    It takes a few minutes to create the instance and for it to be ready for use.

Create a database

Console

  1. In the Cloud console, go to the Cloud SQL Instances page.

    Go to the Cloud SQL Instances page

  2. Select the INSTANCE_NAME instance.

  3. Go to the Databases tab.

  4. Click Create database.

  5. In the Database name dialog, enter DATABASE_NAME.

  6. Click Create.

gcloud

  • Create the database within the recently created instance:

    gcloud sql databases create DATABASE_NAME \
        --instance INSTANCE_NAME
    

    Replace DATABASE_NAME with a name for the database inside the instance.

Create a user

Generate a random password for the database user, and write it to a file called dbpassword:

cat /dev/urandom | LC_ALL=C tr -dc '[:alpha:]'| fold -w 50 | head -n1 > dbpassword

Console

  1. In the Cloud console, go to the Cloud SQL Instances page.

    Go to the Cloud SQL Instances page

  2. Select the INSTANCE_NAME instance.

  3. Go to the Users tab.

  4. Click Add User Account.

  5. Under the Built-in Authentication dialog:

    1. Enter the user name DATABASE_USERNAME.
    2. Enter the content of the dbpassword file as the password PASSWORD.
  6. Click Add.

gcloud

  • Create the user within the recently created instance and set its password to be the content of dbpassword:

    gcloud sql users create DATABASE_USERNAME \
       --instance=INSTANCE_NAME --password=$(cat dbpassword)
    

    Replace DATABASE_USERNAME with a name for the user inside the instance.

Set up a Cloud Storage bucket

You can host Rails static assets and user-uploaded media in highly available object storage using Cloud Storage.

Console

  1. In the Cloud console, go to the Cloud Storage Browser page.

    Go to Browser

  2. Click Create bucket.
  3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
    • For Name your bucket, enter a name that meets the bucket naming requirements.
    • For Location, select the following: us-central1
    • For Choose a default storage class for your data, select the following: Standard.
    • For Choose how to control access to objects, select an Access control option.
    • For Advanced settings (optional), specify an encryption method, a retention policy, or bucket labels.
  4. Click Create.

gcloud

The gsutil command-line tool was installed as part of installing the gcloud CLI.

  • Create a Cloud Storage bucket. To create a unique Cloud Storage bucket name, use the PROJECT_ID and a suffix of your choice, MEDIA_BUCKET_SUFFIX. In Cloud Storage, bucket names must be globally unique.

    gsutil mb -l REGION gs://PROJECT_ID-MEDIA_BUCKET_SUFFIX
    

After creating a bucket, to make the uploaded images public, change the permissions of image objects to be readable by everyone.

Console

  1. In the Google Cloud console, go to the Cloud Storage Browser page.

    Go to Browser

  2. In the list of buckets, click on the name of the bucket that you want to make public.

  3. Select the Permissions tab near the top of the page.

  4. Click the Add members button.

    The Add members dialog box appears.

  5. In the New members field, enter allUsers.

  6. In the Select a role drop down, select the Cloud Storage sub-menu, and click the Storage Object Viewer option.

  7. Click Save.

Once shared publicly, a link icon appears for each object in the public access column. You can click on this icon to get the URL for the object.

To learn how to get detailed error information about failed operations in the Ruby browser, see Troubleshooting.

gcloud

  • Use the gsutil iam ch command to make all objects public. Use the value for MEDIA_BUCKET_SUFFIX that you used when you created the bucket.

    gsutil iam ch allUsers:objectViewer gs://PROJECT_ID-MEDIA_BUCKET_SUFFIX
    

Store secret values in Secret Manager

Now that the backing services are configured, Rails needs secure information, such as passwords, to access these services. Instead of putting these values directly into the Rails source code, this tutorial uses Rails Credentials and Secret Manager to store this information securely.

Create encrypted credentials file and store key as Secret Manager secret

Rails stores secrets in an encrypted file called 'config/credentials.yml.enc'. The file can be decrypted with the local config/master.key or the environment variable ENV[“RAILS_MASTER_KEY”]. In the credentials file, you can store the Cloud SQL instance database password and other access keys for external APIs.

You can store this key securely in Secret Manager. Then, you can grant Cloud Run and Cloud Build access to the key by granting access to their respective service accounts. Service accounts are identified by an email address that contains the project number.

  1. Generate the config/credentials.yml.enc file with the following command:

    bin/rails credentials:edit
    

    The command will create a config/master.key if no master key is defined, and create a config/credentials.yml.enc file if the file does not exist. This will open a temporary file in your default $EDITOR with the decrypted contents for the secrets to be added.

  2. Copy and paste the newly created PostgreSQL instance database password from the dbpassword file into the credentials file:

    secret_key_base: GENERATED_VALUE
    gcp:
      db_password: PASSWORD
    

    Secrets can be accessed with Rails.application.credentials. For example, Rails.application.credentials.secret_key_base should return the application's secret key base and Rails.application.credentials.gcp[:db_passsword] should return your database password.

  3. The config/credentials/yml.enc is stored encrypted, but config/master.key can be stored in Secret Manager.

    Console

    1. In the Cloud console, go to the Secret Manager page.

      Go to the Secret Manager page

    2. Click Create secret

    3. In the Name field, enter a name for the secret RAILS_SECRET_NAME.

    4. In the Secret value dialog, paste the mater.key value into the box.

    5. Click Create secret.

    6. On the Secret details page of your secret, note the project number:

      projects/PROJECTNUM/secrets/RAILS_SECRET_NAME

    7. In the Permissions tab, click Add Member

    8. In the New Members field, enter PROJECTNUM-compute@developer.gserviceaccount.com, and then press Enter.

    9. In the New Members field, enter PROJECTNUM@cloudbuild.gserviceaccount.com, and then press Enter.

    10. In the Role drop-down menu, select Secret Manager Secret Accessor.

    11. Click Save.

    gcloud

    1. Create a new secret with the value of the config/master.key:

      gcloud secrets create RAILS_SECRET_NAME --data-file config/master.key
      

      Replace RAILS_SECRET_NAME with a name for the new secret.

    2. To confirm the creation of the secret, check it:

      gcloud secrets describe RAILS_SECRET_NAME
      
      gcloud secrets versions access latest --secret RAILS_SECRET_NAME
      
    3. Get the value of the project number:

      gcloud projects describe PROJECT_ID --format='value(projectNumber)'
      
    4. Grant access to the secret to the Cloud Run service account:

      gcloud secrets add-iam-policy-binding RAILS_SECRET_NAME \
          --member serviceAccount:PROJECTNUM-compute@developer.gserviceaccount.com \
          --role roles/secretmanager.secretAccessor
      

      Replace PROJECTNUM with the project number value from above.

    5. Grant access to the secret to the Cloud Build service account:

      gcloud secrets add-iam-policy-binding RAILS_SECRET_NAME \
          --member serviceAccount:PROJECTNUM@cloudbuild.gserviceaccount.com \
          --role roles/secretmanager.secretAccessor
      

      In the output, confirm that bindings lists the two service accounts as members.

Connect Rails app to production database and storage

This tutorial uses a PostgreSQL instance as the production database and Cloud Storage as the storage backend. For Rails to connect to the newly created database and storage bucket, you need to specify all the information needed to access them in the .env file. The .env file contains the configuration for the application environment variables. The application will read this file using the dotenv gem. Since the secrets are stored in credentials.yml.enc and Secret Manager, the .env doesn’t have to be encrypted because it doesn’t hold any sensitive credentials.

  1. To configure the Rails app to connect with the database and storage bucket, open the .env file.
  2. Modify the .env file configuration to the following. Use the value of MEDIA_BUCKET_SUFFIX that you used when you created the bucket.

    PRODUCTION_DB_NAME: DATABASE_NAME
    PRODUCTION_DB_USERNAME: DATABASE_USERNAME
    CLOUD_SQL_CONNECTION_NAME: PROJECT_ID:REGION:INSTANCE_NAME
    GOOGLE_PROJECT_ID: PROJECT_ID
    STORAGE_BUCKET_NAME: PROJECT_ID-MEDIA_BUCKET_SUFFIX
    

    The Rails app is now set up to use Cloud SQL and Cloud Storage when deploying to Cloud Run.

Grant Cloud Build access to Cloud SQL

In order for Cloud Build to apply the database migrations, you need to grant permissions for Cloud Build to access Cloud SQL.

Console

  1. In the Cloud console, go to the Identity and Access Management page.

    Go to the Identity and Access Management page

  2. To edit the entry for PROJECTNUM@cloudbuild.gserviceaccount.com member, click Edit Member.

  3. Click Add another role

  4. In the Select a role dialog, select Cloud SQL Client.

  5. Click Save

gcloud

  • Grant permission for Cloud Build to access Cloud SQL:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --member serviceAccount:PROJECTNUM@cloudbuild.gserviceaccount.com \
        --role roles/cloudsql.client
    

Deploying the app to Cloud Run

With the backing services set up, you can now deploy the app as a Cloud Run service.

  1. Using the supplied cloudbuild.yaml, use Cloud Build to build the image, run the database migrations, and populate the static assets:

    gcloud builds submit --config cloudbuild.yaml \
        --substitutions _SERVICE_NAME=SERVICE_NAME,_INSTANCE_NAME=INSTANCE_NAME,_REGION=REGION,_SECRET_NAME=RAILS_SECRET_NAME
    

    Replace SERVICE_NAME with the name for your service. This first build takes a few minutes to complete. If the build timed out, increase the timeout duration by inserting --timeout=2000s into the build command above.

  2. When the build is successful, deploy the Cloud Run service for the first time, setting the service region, base image, and connected Cloud SQL instance:

    gcloud run deploy SERVICE_NAME \
         --platform managed \
         --region REGION \
         --image gcr.io/PROJECT_ID/SERVICE_NAME \
         --add-cloudsql-instances PROJECT_ID:REGION:INSTANCE_NAME \
         --allow-unauthenticated
    

    You should see output that shows the deployment succeeded, with a service URL:

    Service [SERVICE_NAME] revision [SERVICE_NAME-00001-tug] has been deployed
     and is serving 100 percent of traffic at https://SERVICE_NAME-HASH-uc.a.run.app

  3. To see the deployed service, go to the service URL.

    Screenshot of cat album's application landing page.
    If the service URL shows Cat Photo Album, you're on the homepage of the app.

  4. Try to upload a new photo. If the photo successfully uploads, the Rails application has been successfully deployed.

    Screenshot of cat album's application landing page with a photo.

Updating the application

While the initial provisoning and deployment steps were complex, making updates is a simpler process:

  1. Run the Cloud Build build and migration script:

    gcloud builds submit --config cloudbuild.yaml \
         --substitutions _SERVICE_NAME=SERVICE_NAME,_INSTANCE_NAME=INSTANCE_NAME,_REGION=REGION,_SECRET_NAME=RAILS_SECRET_NAME
    
  2. Deploy the service, specifying only the region and image:

    gcloud run deploy SERVICE_NAME \
         --platform managed \
         --region REGION \
         --image gcr.io/PROJECT_ID/SERVICE_NAME
    

Understanding the code

The Rails sample app was created using standard Rails commands. The following commands create the cat_album app and use the scaffold command to generate a model, controller, and views for the Photo resource:

rails new cat_album
rails generate scaffold Photo caption:text

Database connection

The config/database.yml file contains the configuration needed to access your databases in different environments (development, test, production). For example, the production database is configured to run in Cloud SQL for PostgreSQL. The database name and username are set through environment variables in the .env file, while the database password is stored inside the config/credentials.yml.enc file, which requires the RAILS_MASTER_KEY to decrypt.

When the app runs on Cloud Run (fully managed), it connects to the PostgreSQL instance using a socket provided by the Cloud Run environment. When the app runs on your local machine, it connects to the PostgreSQL instance using the Cloud SQL Auth proxy.

production:
  <<: *default
  database: <%= ENV["PRODUCTION_DB_NAME"] %>
  username: <%= ENV["PRODUCTION_DB_USERNAME"] %>
  password: <%= Rails.application.credentials.gcp[:db_password] %>
  host: "<%= ENV.fetch("DB_SOCKET_DIR") { '/cloudsql' } %>/<%= ENV["CLOUD_SQL_CONNECTION_NAME"] %>"

Cloud-stored user uploaded media

Rails uses Active Storage to upload files to cloud storage providers. The config/storage.yml and config/environments/production.rb files specify Cloud Storage as the service provider in the production environment.

google:
  service: GCS
  project: <%= ENV["GOOGLE_PROJECT_ID"] %>
  bucket: <%= ENV["STORAGE_BUCKET_NAME"] %>
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :google

Automation with Cloud Build

The cloudbuild.yaml file performs not only the typical image build steps (creating the container image and pushing that to the container registry), but also the Rails database migrations. These require access to the database, which is performed by using the app-engine-exec-wrapper, a helper for Cloud SQL Auth proxy.

steps:
  - id: "build image"
    name: "gcr.io/cloud-builders/docker"
    entrypoint: 'bash'
    args: ["-c", "docker build --build-arg MASTER_KEY=$$RAILS_KEY -t gcr.io/${PROJECT_ID}/${_SERVICE_NAME} . "]
    secretEnv: ["RAILS_KEY"]

  - id: "push image"
    name: "gcr.io/cloud-builders/docker"
    args: ["push", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"]

  - id: "apply migrations"
    name: "gcr.io/google-appengine/exec-wrapper"
    entrypoint: "bash"
    args:
      [
        "-c",
        "/buildstep/execute.sh -i gcr.io/${PROJECT_ID}/${_SERVICE_NAME} -s ${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME} -e RAILS_MASTER_KEY=$$RAILS_KEY -- bundle exec rails db:migrate"
      ]
    secretEnv: ["RAILS_KEY"]

substitutions:
  _REGION: us-central1
  _SERVICE_NAME: rails-cat-album
  _INSTANCE_NAME: cat-album
  _SECRET_NAME: rails-master-key

availableSecrets:
  secretManager:
  - versionName: projects/${PROJECT_ID}/secrets/${_SECRET_NAME}/versions/latest
    env: RAILS_KEY

images:
  - "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"

Substitution variables are used in this configuration. Changing the values in the file directly means the --substitutions flag can be dropped at migration time.

In this configuration, only existing migrations in the db/migrate directory are applied. To create migration files, see Active Record Migrations.

To build the image and apply migrations, the Cloud Build configuration needs access to the RAILS_MASTER_KEY secret from Secret Manager. The availableSecrets field sets the secret version and environment variables to use for the secret. The master key secret is passed in as an argument in the build image step and then gets set to be the RAILS_MASTER_KEY in the Dockerfile when building the image.

ARG MASTER_KEY
ENV RAILS_MASTER_KEY=${MASTER_KEY}

To extend the Cloud Build configuration to include the deployment in the one configuration without having to run two commands, see Continuous deployment from git using Cloud Build. This requires IAM changes, as described.

Support for Ruby 2.7

Though this tutorial uses Ruby 3.0, it can also support Ruby 2.7. To use Ruby 2.7, change the Ruby base image in the Dockerfile to be 2.7

# Pinning the OS to buster because the nodejs install script is buster-specific.
# Be sure to update the nodejs install command if the base image OS is updated.
FROM ruby:3.0-buster

Cleanup

  1. In the 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.