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.
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
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Cloud SQL, Cloud Build, Secret Manager, and Compute Engine APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Run, Cloud SQL, Cloud Build, Secret Manager, and Compute Engine APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 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.
Clone the repository:
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
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
In the Google Cloud console, go to the Cloud SQL Instances page.
Click Create Instance.
Click Choose PostgreSQL.
In the Instance ID field, enter a name for the instance (
INSTANCE_NAME
).In the Password field, enter a password for the postgres user.
Use the default values for the other fields.
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:
INSTANCE_NAME
: your new Cloud SQL instance nameREGION
: the Google Cloud region
It takes a few minutes to create the instance and for it to be ready for use.
Create a database
Console
In the Google Cloud console, go to the Cloud SQL Instances page.
Select the INSTANCE_NAME instance.
Go to the Databases tab.
Click Create database.
In the Database name dialog, enter
DATABASE_NAME
.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
In the Google Cloud console, go to the Cloud SQL Instances page.
Select the INSTANCE_NAME instance.
Go to the Users tab.
Click Add User Account.
Under the Built-in Authentication dialog:
- Enter the user name
DATABASE_USERNAME
. - Enter the content of the
dbpassword
file as the passwordPASSWORD
.
- Enter the user name
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
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create bucket.
- 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.
- Click Create.
gcloud
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.gcloud storage buckets create gs://PROJECT_ID-MEDIA_BUCKET_SUFFIX \ --location=REGION
After creating a bucket, to make the uploaded images public, change the permissions of image objects to be readable by everyone.
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the list of buckets, click on the name of the bucket that you want to make public.
Select the Permissions tab near the top of the page.
Click the Add members button.
The Add members dialog box appears.
In the New members field, enter
allUsers
.In the Select a role drop down, select the Cloud Storage sub-menu, and click the Storage Object Viewer option.
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 Ruby operations in the Google Cloud console, see Troubleshooting.
gcloud
Use the
gcloud storage buckets add-iam-policy-binding
command to make all objects public. Use the value forMEDIA_BUCKET_SUFFIX
that you used when you created the bucket.gcloud storage buckets add-iam-policy-binding gs://PROJECT_ID-MEDIA_BUCKET_SUFFIX \ --member=allUsers --role=roles/storage.objectViewer
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.
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 aconfig/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.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 andRails.application.credentials.gcp[:db_passsword]
should return your database password.The
config/credentials/yml.enc
is stored encrypted, butconfig/master.key
can be stored in Secret Manager.Console
In the Google Cloud console, go to the Secret Manager page.
Click Create secret
In the Name field, enter a name for the secret
RAILS_SECRET_NAME
.In the Secret value dialog, paste the mater.key value into the box.
Click Create secret.
On the Secret details page of your secret, note the project number:
projects/PROJECTNUM/secrets/RAILS_SECRET_NAME
In the Permissions tab, click Add Member
In the New Members field, enter
PROJECTNUM-compute@developer.gserviceaccount.com
, and then pressEnter
.In the New Members field, enter
PROJECTNUM@cloudbuild.gserviceaccount.com
, and then pressEnter
.In the Role drop-down menu, select Secret Manager Secret Accessor.
Click Save.
gcloud
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.To confirm the creation of the secret, check it:
gcloud secrets describe RAILS_SECRET_NAME gcloud secrets versions access latest --secret RAILS_SECRET_NAME
Get the value of the project number:
gcloud projects describe PROJECT_ID --format='value(projectNumber)'
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.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.
- To configure the Rails app to connect with the database and storage bucket,
open the
.env
file. Modify the
.env
file configuration to the following. Use the value ofMEDIA_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
In the Google Cloud console, go to the Identity and Access Management page.
To edit the entry for
PROJECTNUM@cloudbuild.gserviceaccount.com
member, click Edit Member.Click Add another role
In the Select a role dialog, select Cloud SQL Client.
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.
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.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.appTo see the deployed service, go to the service URL.
Try to upload a new photo. If the photo successfully uploads, the Rails application has been successfully deployed.
Updating the application
While the initial provisoning and deployment steps were complex, making updates is a simpler process:
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
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.
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.
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.
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.
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
Cleanup
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.