
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Set up a Cloud SQL for PostgreSQL instance
/ 25
Set up a Cloud Storage bucket
/ 25
Create encrypted credentials file and store key as Secret Manager secret
/ 25
Deploy the app to Cloud Run
/ 25
In this lab, you will 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.
Google Cloud SQL for PostgreSQL is a fully-managed database service that makes it easy to set up, maintain, manage, and administer your PostgreSQL relational databases on Google Cloud.
Cloud Run is a managed compute platform that enables you to run containers that are invocable via requests or events. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.
Deploying Rails applications involves integrating multiple services together to form a cohesive project. Here's a visual example of the application you will deploy for this lab:
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.
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
The code for the Rails sample app is in the GoogleCloudPlatform/ruby-docs-samples repository on GitHub.
This lab 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.
Rails supports multiple relational databases, including several offered by Cloud SQL. This lab 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.
dbpassword
:qwiklabs_user
) within the recently created instance and set its password to be the content of dbpassword:Click Check my progress to verify the objective.
You can host Rails static assets and user-uploaded media in highly available object storage using Cloud Storage.
Click Check my progress to verify the objective.
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 lab uses Rails Credentials and Secret Manager to store this information securely.
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.
config/credentials.yml.enc
file with the following command: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 the assigned $EDITOR with the decrypted contents for the secrets to be added.
Do not close the editor yet. You will get the db_password value next.
Open a new tab in your Cloud Shell by pressing the + button.
Run this command to review the password saved in your dbpassword
file:
Copy your output from the previous command.
Return to your first Cloud Shell tab with your credentials file open in an editor.
Replace the REPLACE_WITH_DB_PASSWORD value with the password you copied.
Your credentials file should look similar to this:
EDITOR="nano" bin/rails credentials:edit
to edit your credentials file again. This command should be run in the ~/ruby-docs-samples/run/rails
directory.
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_password]
should return your database password.
control
+ x
and then y
to save and exit your credentials.The config/credentials/yml.enc
is stored encrypted, but config/master.key
can be stored in Secret Manager.
config/master.key
:If prompted to enable API service, enter Y
This lab 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.
Our .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 Manger, the .env
doesn’t have to be encrypted because it doesn’t hold any sensitive credentials.
.env
file:You'll notice there are key value pairs which you can define to set up your connection.
You filled out your .env
file with the variables you used to store the values for your database, project, region, instance, and bucket earlier in the lab.
Click Check my progress to verify the objective.
With the backing services set up, you can now deploy the app as a Cloud Run service.
These commands store a variable with the major and minor versions of your Ruby version and then reconstruct the line that specifies which image to pull using your Ruby version instead.
cloudbuild.yaml
, use Cloud Build to build the image, run the database migrations, and populate the static assets: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.
You should see output that shows the deployment succeeded, with a service URL:
If the service URL shows Cat Photo Album, you're on the homepage of the app.
If the photo successfully uploads, the Rails application has been successfully deployed.
Click Check my progress to verify the objective.
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:
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 via a socket provided by the Cloud Run environment. When the app runs on your local machine, it connects to the PostgreSQL instance via Cloud SQL Auth proxy.
run/rails/config/database.yml
:
Rails uses Active Storage to upload files to cloud storage providers.The config/storage.yml
and config/environments/production.rb
files specify Google Cloud Storage as the service provider in the production environment.
run/rails/config/storage.yml
:
run/rails/config/environments/production.rb
:
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.
run/rails/cloudbuild.yaml
:
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.
run/rails/Dockerfile
:
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.
You learned how to use Cloud SQL for PostgreSQL using Ruby on Rails and then deploy it on the Cloud Run environment.
Continue your learning with the given docs:
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 19, 2025
Lab Last Tested February 19, 2025
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
このコンテンツは現在ご利用いただけません
利用可能になりましたら、メールでお知らせいたします
ありがとうございます。
利用可能になりましたら、メールでご連絡いたします
One lab at a time
Confirm to end all existing labs and start this one