The Ruby runtime

Your Cloud Run function runs in an environment consisting of an operating system version with add-on packages, language support, and the Ruby Functions Framework library that supports and invokes your function. This environment is identified by the language version, and is known as the runtime ID.

Function preparation

You can prepare a function directly from the Google Cloud console or write it on your local machine and upload it. To prepare your local machine for Ruby development, see Set up a Ruby development environment.

Select your runtime

The latest Ruby base image target that Cloud Run supports is ruby33. For the full list of supported runtime versions, and their corresponding base image targets, see Supported language runtimes and base images. You can select the preferred Ruby runtime for your function during deployment.

You can select a runtime version using the Google Cloud console, or the gcloud CLI. Click the tab for instructions on using the tool of your choice:

gcloud

Specify the Ruby base image for your function using the --base-image flag, while deploying your function. For example:

gcloud beta run deploy FUNCTION \
    --source . \
    --function FUNCTION_ENTRYPOINT \
    --base-image ruby33

Replace:

  • FUNCTION with the name of the function you are deploying. You can omit this parameter entirely, but you will be prompted for the name if you omit it.

  • FUNCTION_ENTRYPOINT with the entry point to your function in your source code. This is the code Cloud Run executes when your function runs. The value of this flag must be a function name or fully-qualified class name that exists in your source code.

For detailed instructions on deploying a function using the gcloud CLI, see Deploy functions in Cloud Run.

Console

You can select a runtime version when you create or update a Cloud Run function in the Google Cloud console. For detailed instructions on deploying a function, see Deploy functions in Cloud Run.

To select a runtime in the Google Cloud console when you create a function, follow these steps:

  1. In the Google Cloud console, go to the Cloud Run page:

    Go to Cloud Run

  2. Click Write a function.

  3. In the Runtime list, select a Ruby runtime version.

  4. Click Create, and wait for Cloud Run to create the service using a placeholder revision.

  5. The console will redirect you to the Source tab where you can see the source code of your function. Click Save and redeploy.

For detailed instructions on updating the runtime version after your function is deployed, see Re-deploy new source code.

Source code structure

For Cloud Run functions to find your function's definition, your source code must follow a specific structure. See Write Cloud Run functions for more information.

Specify dependencies

Cloud Run functions written in Ruby use bundler to access dependencies.

The Functions Framework is a required dependency for all functions. Although Cloud Run functions installs it on your behalf when the function is created, we recommend that you include it as an explicit dependency for clarity.

If your function relies on private dependencies, we recommend that you mirror functions-framework to your private registry. Include the mirrored functions-framework as a dependency to your function to avoid installing the package from the public internet.

Each function must provide a Gemfile that specifies the functions_framework gem, along with any additional gems needed by the function. Gemfile must be in the same directory as the app.rb file that contains your function code. In addition, your function must provide a lockfile that specifies all the transitive dependencies and their exact versions. This file, Gemfile.lock, is also located in the same directory alongside the Gemfile.

When you deploy your function, Cloud Run downloads and installs the dependencies declared in the Gemfile and Gemfile.lock using bundler.

The Gemfile lists the packages required by your function, along with any optional version constraints. For more details, see the Gemfile reference.

The following is an example Gemfile:

source "https://rubygems.org"

gem "functions_framework", "~> 0.7"
gem "google-cloud-storage", "~> 1.29"

Packaging local dependencies

You can also package and deploy dependencies alongside alongside your function. This approach is useful if your dependency is not available using the rubygems package manager.

To package a gem locally, include it in a directory in your function's directory structure, and provide the path in the dependency's Gemfile entry. The gem directory must include a valid gemspec file, and it must be located within the function's directory hierarchy so that its code is deployed along with your function. For example, you might use a directory structure such as the following:

myfunction/
├── Gemfile
├── Gemfile.lock
├── app.rb
└── my_private_gem/
    ├── lib/
    |   └── my_private_gem.rb
    └── my_private_gem.gemspec

The Gemfile entry might look like this:

source "https://rubygems.org"

gem "functions_framework", "~> 0.7"
gem "my_private_gem", path: "./my_private_gem"

See the Gemfile reference for more discussion about referencing local gem paths.