Deploy Cloud Functions with version control

This page shows you how to deploy Cloud Functions using code that is version controlled in Cloud Source Repositories.

By integrating Cloud Functions with Cloud Source Repositories, you can use Cloud Source Repositories to version control the code that contains your function. As the function changes over time, you can continue to access previous commits to see how the function changed and when.

Before you begin

  1. Complete the steps from Create a code repository in Cloud Source Repositories.
  2. Enable the Cloud Functions API and Cloud Build API.
  3. Enable the Cloud Functions API and Cloud Build API

Add a function to the repository

  1. On your local machine, go to the root directory of the hello-world repository:

    cd hello-world
    
  2. On your local system, create a directory for the function code.

    Linux or macOS

    Create the directory:

    mkdir gcf_hello_world

    Move into the directory:

    cd gcf_hello_world

    Windows(CMD)

    Create the directory:

    mkdir %HOMEDRIVE%%HOMEPATH%\gcf_hello_world

    Move into the directory:

    cd %HOMEDRIVE%%HOMEPATH%\gcf_hello_world
  3. In the gcf_hello_world directory, create an index.js file with the following contents:

    /* HTTP Cloud Function.
    *
    * @param {Object} req Cloud Function request context.
    * @param {Object} res Cloud Function response context.
    */
    exports.helloGET = (req, res) => {
      res.send('Hello from Cloud Functions and Cloud Source Repositories');
    };
    

    This is a simple function named helloGET that responds to HTTP GET requests with the text Hello from Cloud Functions and Cloud Source Repositories.

Push to Cloud Source Repositories

Push the files you just created into Cloud Source Repositories.

  1. Add the files:

    git add .
    
  2. Commit the files with a comment describing the history of this action:

    git commit -m "Add Cloud Functions test to Cloud Source Repositories"
    
  1. Using the git push command, add the contents of the local Git repository to Cloud Source Repositories:

    git push origin master
    

Create and deploy the function

Using either the Google Cloud console or the Google Cloud CLI, create and deploy your function.

Console

  1. Go to the Cloud Functions page.

    Go to the Cloud Functions page

    Make sure that the Google Cloud project for which you enabled Cloud Functions is selected.

  2. Click Create function.

  3. In the Create function page, fill out the following options:

    • In the Environment field, select 1st gen.
    • In the Function name field, type cloud-source-repositories-test.
    • In the Region field select us-central1.
    • In the Trigger list, select HTTP.
    • In the Authentication list, select Require authentication.
    • Ensure that the Require HTTPS option is checked.
  4. Click Save.

  5. Click Next.

    • In the Runtime field, select Node.js 16.
    • In the Entry point field, enter helloGET
    • In the Source code list, select Cloud source repository.
    • In the Repository field, type hello-world.
    • In the Branch name field, type master.
    • In the Directory with source code field, type /gcf_hello_world.
  6. Click Deploy.

While the function is being deployed, a small spinner appears next to it. When the deployment is finished, the spinner becomes a green check mark.

gcloud CLI

  1. In a terminal window, set a variable that contains your Google Cloud project ID. Make sure this is the same Google Cloud project that contains your repository.

    export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
    
  2. Deploy the function:

    gcloud functions deploy helloGET \
    --source https://source.developers.google.com/projects/$PROJECT_ID/repos/hello-world/moveable-aliases/master/paths/gcf_hello_world \
    --trigger-http \
    --runtime=nodejs8;
    

For more information on deploying to Cloud Source Repositories, see Deploying from source control.

Test the function

Using either the GCP Console or the gcloud CLI, test your new function.

Console

  1. Go to the Cloud Functions Overview page.

    Go to the Overview page

    Make sure that the Google Cloud project for which you enabled Cloud Functions is selected.

  2. Click the Show actions menu in the cloud-source-repositories-test function row.

  3. Click Test function.

  4. Click the Test the function button.

    After a minute or two, the Output field updates to display the message Hello from Cloud Functions and Cloud Source Repositories.

gcloud CLI

In a terminal window, enter the following command:

gcloud functions call helloGET

Output similar to the following is displayed:

executionId: owqd9fdh5od2
result: Hello from Cloud Functions and Cloud Source Repositories

Clean up

To delete the function and repository you created, follow these steps.

Delete the function

  1. Go to the Cloud Functions Overview page.

    Go to the Overview page

    Make sure that the Google Cloud project for which you enabled Cloud Functions is selected.

  2. Select the helloGET function for this quickstart, cloud-source-repositories-test.

  3. On the same row, click More , and then click Delete.

Delete the repository

  1. In the GCP Console, open the All repositories page for Cloud Source Repositories.

    Open Cloud Source Repositories

  2. Hold the pointer over the repository you want to delete and click Settings .

    The General settings page opens.

  3. Click Delete this repository .

    The Remove repository dialog opens.

  4. Type the name of the repository you want to delete.

  5. Click Delete.

What's next