Quickstart Deploying to Cloud Functions from Cloud Source Repositories

This topic describes 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 Quickstart Creating a Repository.

  2. Enable the Cloud Functions API.

    Enable the API

Add a function to the repository

  1. Navigate to the root directory of the hello-world repository on your local machine.

    cd hello-world/
    
  2. Create a directory on your local system for the function code:

    Linux or Mac OS X

    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. Create an index.js file in the gcf_hello_world directory with the following contents. This is a simple function named helloGET that responds to GET requests via HTTP with the text Hello from Cloud Functions and Cloud Source Repositories.

    /* 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');
    };
    

Push to Cloud Source Repositories

In this step, you push the files you just created into Cloud Source Repositories.

  1. Add the files:

       git add .
    
  2. Commit the file:

       git commit -m "Add Cloud Function test to Cloud Source Repositories"
    
  3. Add the contents of the local Git repository to Cloud Source Repositories using the git push command:

       git push origin master
    

Create and Deploy the function

At this point, you can create and deploy your function. You can perform these tasks either with the GCP Console or the Cloud SDK.

GCP Console

  1. Go to Cloud Functions Overview page in the GCP Console.

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

  2. Click Create function.

    Screenshot that shows how to start function creation process

  3. Name your function, cloud-source-repositories-test,

  4. In the Trigger field, select HTTP Trigger.

  5. In the Source code field, select Cloud Source Repository.

  6. In the Repository field, type hello-world.

  7. From the Branch/Tag options, select Branch.

  8. In the Branch Name field, type master.

  9. In the Directory with Source Code field, type /gcf_hello_world.

  10. In the Function to Execute field, type helloGET.

  11. Click Create.

While the function is being deployed, the icon next to it is a small spinner. After it finishes deploying, the spinner turns to a green check mark.

Cloud SDK

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

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

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

For more information on deploying to Cloud Source Repositories, see Deploying from Source Control in the Cloud Functions documentation.

Test the function

You can deploy your new function using either the GCP Console or the Cloud SDK.

GCP Console

  1. Go to Cloud Functions Overview page in the GCP Console.

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

  2. Click the helloGET function.

    The Function Details screen opens.

  3. Click the Testing tab.

  4. Click Test this function.

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

Cloud SDK

From a terminal window, type the following command:

gcloud beta functions call helloGET

You should receive a response similar to the following:

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

Clean up

To delete the repository you created:

  1. Open the source view in the GCP Console:

    Open the Source View in the Console

  2. Click the More button More button next to the repository you want to delete.

    Create Repository button

  3. Select Delete.

  4. When prompted for the repository name, enter hello-world and then click Delete.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Source Repositories