Deploying to Cloud Functions

This page explains how to deploy applications to Cloud Functions using Cloud Build. If you're new to Cloud Build, read the quickstarts and the Build configuration overview first.

Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud 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 Functions, read Cloud Functions documentation.

Before you begin

  • Enable the Cloud Functions API:

    Enable the Cloud Functions API

  • Grant the Cloud Functions Developer role to the Cloud Build service account:

    1. Open the Cloud Build Settings page:

      Open the Cloud Build Settings page

    2. Set the status of the Cloud Functions Developer role to Enabled.

  • To run the gcloud commands in this page, install the gcloud command-line tool.

  • Keep your application source code that you want to build and deploy to Cloud Functions handy.

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 step in the Cloud Build config file.

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

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

  1. In your project root directory, create the Cloud Build configuration file named 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.
    • --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
    

    JSON

    {
     "steps": [
      {
         "name": "gcr.io/google.com/cloudsdktool/cloud-sdk",
         "args": [
           "gcloud",
           "functions",
           "deploy",
            [
              "FUNCTION_NAME"
            ],
            "--region=FUNCTION_REGION",
            "--source=.",
            "--trigger-http"
         ]
       }
      ]
    }
    

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

    • FUNCTION_NAME is the name of the Cloud Functions you are deploying.
    • FUNCTION_REGION is the region to which you're deploying Cloud Functions. For a list of supported regions, see Cloud Functions locations.
    • --trigger-http is the trigger type for this function, in this case an HTTP request (webhook).

    For more information on using gcloud functions deploy, see the Cloud Functions documentation.

  3. Start the build using the config file created in the previous step:

     gcloud builds submit --config CONFIG_FILE_PATH SOURCE_DIRECTORY
    

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

    • CONFIG_FILE_PATH is the path to the build config file.
    • SOURCE_DIRECTORY is the path or URL to the source code.

    If you don't specify a CONFIG_FILE_PATH and SOURCE_DIRECTORY in the gcloud builds submit command, Cloud Build assumes that the config file and the source code are in the current working directory.

Continuous deployment

You can automate the deployment of your software to Cloud 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 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
    

    JSON

    {
     "steps": [
      {
         "name": "gcr.io/google.com/cloudsdktool/cloud-sdk",
         "args": [
           "gcloud",
           "functions",
           "deploy",
            [
              "FUNCTION_NAME"
            ],
            "--region=FUNCTION_REGION",
            "--source=.",
            "--trigger-http"
         ]
       }
      ]
    }
    

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

    • FUNCTION_NAME is the name of the Cloud Functions you are deploying.
    • FUNCTION_REGION is the region to which you're deploying Cloud Functions. For a list of supported regions, see Cloud Functions locations.
    • --trigger-http is the trigger type for this function, in this case an HTTP request (webhook).
  2. 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 Open.

    4. Click Create trigger.

      On the Create trigger page, enter the following settings:

      1. Enter a name for your trigger.

      2. Select the repository event to start your trigger.

      3. Select the repository that contains your source code and build config file.

      4. Specify the regex for the branch or tag name that will start your trigger.

      5. Configuration: Choose the build config file you created previously.

    5. 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 Functions.

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

What's next