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

Cloud Run functions supports several versions of Ruby, listed on the Supported language runtimes and base images page. You can select the preferred Ruby runtime for your function during deployment.

gcloud

To use Cloud Run functions to deploy an HTTP function using the gcloud CLI, see Deploy using the Google Cloud CLI.

Console

If you're using the Google Cloud console, select the runtime when you create and deploy your function. See the Google Cloud console quickstart for detailed instructions.

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.