Deploy services from source code

This page describes how to deploy a new service or service revision to Cloud Run directly from source code using a single gcloud CLI command, gcloud run deploy with the --source flag. For an example walkthrough of deploying a Hello World service, see Deploy from source quickstarts.

Behind the scenes, this command uses Google Cloud's buildpacks and Cloud Build to automatically build container images from your source code without having to install Docker on your machine or set up buildpacks or Cloud Build. By default, Cloud Run uses the default machine type provided by Cloud Build. Running gcloud run deploy --source eliminates the need to also run the gcloud builds submit command.

Note that source deployments use Artifact Registry to store built containers. If your project doesn't already have an Artifact Registry repository with the name cloud-run-source-deploy in the region you are deploying to, this feature automatically creates an Artifact Registry repository with the name cloud-run-source-deploy.

If a Dockerfile is present in the source code directory, the uploaded source code is built using that Dockerfile. If no Dockerfile is present in the source code directory, Google Cloud's buildpacks automatically detects the language you are using and fetches the dependencies of the code to make a production-ready container image, using a secure base image managed by Google.

By default, security fixes are only applied when the Cloud Run service is deployed. When you enable automatic security updates for a service, that service receives patches automatically with zero downtime. Learn more about configuring security updates.

Supported languages

In addition to sources with a Dockerfile, deploying from source supports the following languages using Google Cloud's buildpacks:

Runtime Source deployment Buildpack configuration
Go Deploy a Go service Configure Go buildpacks
Node.js Deploy a Node.js service Configure Node.js buildpacks
Python Deploy a Python service Configure Python buildpacks
Java
(includes Kotlin, Groovy, Scala)
Deploy a Java service Configure Java buildpacks
.NET Deploy a .NET service Configure .NET buildpacks
Ruby Deploy a Ruby service Configure Ruby buildpacks
PHP Deploy a PHP service Configure PHP buildpacks

Read more details about supported language versions.

Limitations of this feature

  • Deploy from source uses Artifact Registry and Cloud Build, so this feature is only available in regions supported by Artifact Registry and Cloud Build.
  • Deploying from source is a convenience feature, and does not allow full customization of the build. For more control, build the container image using Cloud Build, for example, using gcloud builds submit, and then deploy the container image using, for example, gcloud run deploy --image.
  • Deploying from source with Google Cloud's buildpacks sets the Last Modified Date of source files to Jan 1, 1980. This is the default behavior of buildpacks and is designed to support reproducible builds. Depending on your language framework, this can affect browser-side caching of static files. If your application is affected by this, Google recommends disabling etag and Last-Modified HTTP headers in your application.
  • Deploying from source with Google Cloud's buildpacks always uses gcr.io/buildpacks/builder:latest. If your preferred language or OS configuration is not available in latest, use a specific builder to create an application image using your preferred builder.
  • You can deploy your service from source using Kotlin and other JVM languages such as Java. The language you use must conform to the following rules:

    • You can build the application using Maven or Gradle.
    • The build file contains all the plugins required to product classes.

Before you begin

  • Make sure you have set up a new project for Cloud Run as described in the setup page.
  • If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.

  • Enable the Cloud Run Admin API and Cloud Build APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

    After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.

Required roles

You or your administrator must grant the deployer account and the Cloud Build service account the following IAM roles.

Click to view required roles for the deployer account

To get the permissions that you need to build and deploy from source, ask your administrator to grant you the following IAM roles:

Click to view required roles for the Cloud Build service account

Cloud Build automatically uses the Compute Engine default service account as the default Cloud Build service account to build your source code and Cloud Run resource, unless you override this behavior. For Cloud Build to build your sources, ask your administrator to grant Cloud Run Builder (roles/run.builder) to the Compute Engine default service account on your project:

  gcloud projects add-iam-policy-binding PROJECT_ID \
      --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
      --role=roles/run.builder
  

Replace PROJECT_NUMBER with your Google Cloud project number, and PROJECT_ID with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.

Granting the Cloud Run builder role to the Compute Engine default service account takes a couple of minutes to propagate.

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Deploy from source

To deploy from source code, click the tab for instructions on using the tool of your choice.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Change to your source directory. The source directory uses a Dockerfile if present, although it's not required.

  3. Build and deploy your service:

    gcloud run deploy SERVICE --source .

    Replace SERVICE with the name you want for your service.

    Optionally, if your source code uses one of the supported language runtimes and you want to configure automatic base image updates for your runtime, specify the --automatic-updates flag and the --base-image flag with the base image for your service, for example nodejs22.

  4. Respond to any prompts to install required APIs by responding y when prompted. You only need to do this once for a project. Respond to other prompts by supplying the platform and region, if you haven't set defaults for these as described in the setup page.

  5. Wait for the build and deploy to complete. When finished, Cloud Run displays a success message.

After deployment, this service revision serves 100% of traffic.

Cloud Code

To deploy from source using Cloud Code, read the IntelliJ and Visual Studio Code guides.

Gemini CLI

Use the /deploy command in the Gemini CLI tool to deploy a Cloud Run service from source code.

To use the Gemini CLI with the Cloud Run Model Context Protocol (MCP) server extension, follow these steps:

  1. Install the latest version of the Gemini CLI in one of the following development environments:

    • Terminal
    • Cloud Shell
    • VS Code using Gemini Code Assist agent mode (see the "VS Code" tab)
  2. Install the Cloud Run extension:

    gemini extensions install https://github.com/GoogleCloudPlatform/cloud-run-mcp
  3. Sign in to the Google Cloud CLI:

    gcloud auth login
  4. Set up Application Default Credentials:

    gcloud auth application-default login
  5. Change to your source code directory.

  6. Launch the Gemini CLI:

    gemini
  7. Build and deploy your service:

    /deploy
    • If you are prompted to provide the Google Cloud project, enter your project name.
    • If you are prompted to select a tool, select deploy_local_folder.
  8. Wait for the build and deploy to complete. When finished, Cloud Run displays a success message.

VS Code

To deploy a Cloud Run service from source code using VS Code with Gemini Code Assist agent mode, see how to use the /deploy command.

To make the Cloud Run Model Context Protocol (MCP) server available for use in Gemini Code Assist agent mode, add the following configuration in your Gemini settings JSON file:

"mcpServers":{
  "cloud-run": {
    "command": "npx",
    "args": ["-y", "@google-cloud/cloud-run-mcp"]
  }
}

Automating building from source

As a best practice for avoiding unversioned changes in local source, Google recommends that you automatically deploy when changes are pushed to your Git repository. To make this easier, you can connect and configure continuous deployment to your Cloud Run service. By connecting your GitHub repositories to Cloud Run, you can configure builds and deploy your repositories without writing Dockerfiles or build files.

To configure automated builds, set up automation as described in the continuous builds page, making sure you choose the option for building source with buildpacks.

What's next

After you deploy a Cloud Run service, you can do the following:

Learn about the source deploy configurations:

You can automate the builds and deployments of your Cloud Run services using Cloud Build triggers: