Deploying to Cloud Run functions (original) (raw)

This page explains how to deploy applications to Cloud Run functions using Cloud 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 and connecting cloud services. With Cloud Run functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired. For more information on Cloud Run functions, readCloud Run functions documentation.

Before you begin

Configuring the deployment

Cloud Build enables you to use any publicly available container image to execute your tasks. You can do this by specifying the image in a build stepin the Cloud Build config file.

Cloud Run functions provides the gcloud functions deploy command, which deploys your function from the directory containing your function code. You can use thecloud-sdk imageas a build step in your config file to invoke gcloud commands within the image. Arguments passed to this build step are passed to Google Cloud CLI directly, allowing you to run any gcloud command in this image.

To deploy an application to Cloud Run functions, use the following steps:

  1. In your project root directory, create theCloud Build configuration filenamed cloudbuild.yaml or cloudbuild.json.
  2. In the config file:
    • Add a name field and specify the gcloud build step.
    • Add functions deploy to the args field to invoke the gcloud functions deploy command. For available configuration options, see gcloud functions deploy reference.
    • --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=RUNTIME  

JSON

{  
 "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:

  1. Start the build using the config file created in the previous step:
 gcloud builds submit --region=REGION --config CONFIG_FILE_PATH SOURCE_DIRECTORY  

Replace the placeholder values in the config file above with the following:

Continuous deployment

You can automate the deployment of your software to Cloud Run functions by creating Cloud Build triggers. You can configure your triggers to build and deploy images whenever you update your source code.

To automate your deployment to Cloud Run functions:

  1. In your repository root, add a config file with steps to invoke the gcloud functions deploy command:

YAML

steps:  
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'  
  args:  
  - gcloud  
  - functions  
  - deploy  
  - FUNCTION_NAME  
  - --region=FUNCTION_REGION  
  - --source=.  
  - --trigger-http  
  - --runtime=RUNTIME  

JSON

{  
 "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:

  1. Create a build trigger with the config file created in the previous step:
    1. Open the Triggers page in the Google Cloud console:
      Open Triggers page
    2. Select your project from the project selector drop-down menu at the top of the page.
    3. Click Create Trigger.
    4. In the Name field, enter a name for your trigger.
    5. Under Region, select the region for your trigger.
    6. Under Event, select the repository event to start your trigger.
    7. Under Source, select your repository and the branch or tag name that will start your trigger. For more information on specifying which branches to autobuild, seeCreating a build trigger.
    8. Under Configuration, select Cloud Build configuration file (YAML or JSON).
    9. In the Cloud Build configuration file location field, type cloudbuild.yaml after the /.
    10. Click Create to save your build trigger.

Anytime you push new code to your repository, you will automatically trigger a build and deploy on Cloud Run functions.

For more information on creating Cloud Build triggers, seeCreating and managing build triggers.

What's next