Quickstart: Deploy 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: Create a repository.
  2. Enable the Cloud Functions API.
  3. Enable the Cloud Functions 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"
    
  3. 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 Cloud SDK, 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 Name field, type cloud-source-repositories-test.
    • In the Trigger list, select HTTP.
    • In the Source code list, select Cloud Source repository.
    • In the Repository field, type hello-world.
    • In the Branch/tag list, select Branch.
    • In the Branch name field, type master.
    • In the Directory with source code field, type /gcf_hello_world.
    • In the Function to execute field, type helloGET.
  4. Click Create.

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.

Cloud SDK

  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 Cloud SDK, 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 cloud-source-repositories-test, the name of the helloGET function you created earlier.

    The Function details page opens.

  3. Click the Testing tab.

  4. Click Test the function.

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

Cloud SDK

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

Bu sayfayı yararlı buldunuz mu? Lütfen görüşünüzü bildirin:

Şunun hakkında geri bildirim gönderin...

Cloud Source Repositories Documentation