Deploying to Cloud Run functions Stay organized with collections Save and categorize content based on your preferences.
This page explains how to deploy applications to Cloud Run functions usingCloud Build. If you're new to Cloud Build, read thequickstarts and theBuild configuration overview first.
Cloud Run functions is a serverless execution environment for building andconnecting cloud services. With Cloud Run functions, you can write single-purposefunctions that are attached to events emitted from your cloud infrastructure andservices. Your function is triggered when an event being watched is fired. Formore information on Cloud Run functions, readCloud Run functions documentation.
Before you begin
Enable the Cloud Run functions API:
Have your application source code ready. Your source code needs to be storedin a repository such as GitHub or Bitbucket.
To run the
gcloudcommands in this page, installtheGoogle Cloud CLI.
Caution: Effective June 17, 2024, Cloud Source Repositories isn't available to new customers. If your organization hasn't previously used Cloud Source Repositories, you can't enable the API or use Cloud Source Repositories. New projects not connected to an organization can't enable the Cloud Source Repositories API. Organizations that have used Cloud Source Repositories prior to June 17, 2024 are not affected by this change.
Required Identity and Access Management permissions
In the Google Cloud console, go to thesettings Cloud BuildPermissions page:
For yourspecified Cloud Build service accountordefault Cloud Build service account, set the status of the following roles toEnabled:
- Cloud Functions Developer (
roles/cloudfunctions.developer) | To deploy and manage functions.- In the Assign Service Account User Role panel,choose a service account to impersonate and then clickGrant Permission.
- Cloud Run Admin (
roles/run.admin) | Allows your Cloud Build service account to deploy new versions to Cloud Run. - Storage Admin (
roles/storage.admin) | Enables reading and writing from Cloud Storage. - Artifact Registry Writer (
roles/artifactregistry.writer) | Allows pulling images from and writing to Artifact Registry. - Logs Writer (
roles/logging.logWriter) | Allows log entries to be written to Cloud Logging. - Cloud Build Editor (
roles/cloudbuild.builds.editor) | Allows your service account to run builds.
- Cloud Functions Developer (
Configuring the deployment
Cloud Build lets you use any publicly available container imageto execute your tasks. You can do this by specifying the image in a buildstepin the Cloud Build config file.
Cloud Run functions provides thegcloud functions deploy command, whichdeploys your function from the directory containing your function code.You can use thecloud-sdk image as a build step in your config file to invokegcloud commands within the image.Arguments passed to this build step are passed to Google Cloud CLI directly,allowing you to run anygcloud command in this image.
To deploy an application to Cloud Run functions, use the following steps:
- In your project root directory, create theCloud Build configuration filenamed
cloudbuild.yamlorcloudbuild.json. In the config file:
- Add a
namefield and specify thegcloudbuild step. - Add
functions deployto theargsfield to invoke thegcloud functions deploycommand.For available configuration options, seegcloud functions deployreference. --source=.implies that the source code is in the current working directory.
YAML
steps:-name:'gcr.io/google.com/cloudsdktool/cloud-sdk'args:-gcloud-functions-deploy-FUNCTION_NAME---region=FUNCTION_REGION---source=.---trigger-http---runtime=RUNTIMEJSON
{"steps":[{"name":"gcr.io/google.com/cloudsdktool/cloud-sdk","args":["gcloud","functions","deploy",["FUNCTION_NAME"],"--region=FUNCTION_REGION","--source=.","--trigger-http","--runtime=RUNTIME"]}]}Replace the placeholder values in the config file above with the following:
FUNCTION_NAMEis the name of the Cloud Run functions you are deploying.If you're updating an existing function, this value must match the name ofthe function you're updating.FUNCTION_REGIONis the region to which you'redeploying Cloud Run functions. For a list of supported regions, seeCloud Run functions locations.--trigger-httpis the trigger type for this function, in this case an HTTP request (webhook).RUNTIMEis theruntimein which to run the function.
For more information on using
gcloud functions deploy, see theCloud Run functions documentation.- Add a
Start the build using the config file created in the previous step:
gcloudbuildssubmit--region=REGION--configCONFIG_FILE_PATHSOURCE_DIRECTORYReplace the placeholder values in the config file above with the following:
CONFIG_FILE_PATHis the path to the build config file.SOURCE_DIRECTORYis the path or URL to the source code.REGIONis one of thesupported build regions.
If you don't specify a
CONFIG_FILE_PATHandSOURCE_DIRECTORYin thegcloud builds submitcommand, Cloud Build assumes that the configfile and the source code are in the current working directory.
Continuous deployment
You can automate the deployment of your software to Cloud Run functions by creatingCloud Build triggers. You can configure your triggers to build anddeploy images whenever you update your source code.
To automate your deployment to Cloud Run functions:
In your repository root, add a config file with stepsto invoke the
gcloud functions deploycommand:YAML
steps:-name:'gcr.io/google.com/cloudsdktool/cloud-sdk'args:-gcloud-functions-deploy-FUNCTION_NAME---region=FUNCTION_REGION---source=.---trigger-http---runtime=RUNTIMEJSON
{"steps":[{"name":"gcr.io/google.com/cloudsdktool/cloud-sdk","args":["gcloud","functions","deploy",["FUNCTION_NAME"],"--region=FUNCTION_REGION","--source=.","--trigger-http","--runtime=RUNTIME"]}]}Replace the placeholder values in the config file above with the following:
FUNCTION_NAMEis the name of the Cloud Run functions you are deploying.If you're updating an existing function, this value must match the name ofthe function you're updating.FUNCTION_REGIONis the region to which you're deploying Cloud Run functions.For a list of supported regions, seeCloud Run functions locations.--trigger-httpis the trigger type for this function, in this case an HTTP request (webhook).RUNTIMEis theruntimein which to run the function.
Create a build trigger with the config file created in the previous step:
Open theTriggers page in the Google Cloud console:
Select your project from the project selector drop-down menu at the top of the page.
ClickCreate Trigger.
In theName field, enter a name for your trigger.
UnderRegion, select theregion for your trigger.
Note: The region for your trigger must match the regionof the function you're deploying to.UnderEvent, select the repository event to start your trigger.
UnderSource, select your repository and the branch or tag namethat will start your trigger.For more information on specifying which branches to autobuild, seeCreating a build trigger.
UnderConfiguration, selectCloud Buildconfiguration file (YAML or JSON).
In theCloud Build configuration file location field,type
cloudbuild.yamlafter the/.ClickCreate to save your build trigger.
Anytime you push new code to your repository, you will automatically trigger abuild and deploy on Cloud Run functions.
For more information on creating Cloud Build triggers, seeCreating and managing build triggers.
What's next
- Learn how toperform blue-green deployments on Compute Engine
- Learn how todeploy on Cloud Run
- Learn how todeploy on GKE
- Learn how todeploy on Firebase
- Learn how todeploy on App Engine
- Learn how totroubleshoot build errors.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.