This topic describes how to deploy Cloud Functions by 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.
Create and deploy the function
With your code stored in Cloud Source Repositories, your next step is to create a function and deploy it. You can perform these tasks either with the Google Cloud Console or the Cloud SDK.
Go to the Cloud Functions page.
Ensure the Google Cloud project for which you enabled Cloud Functions is selected.
Click Create function.
On the Create function page, fill out the following options:
- In the Name field, type a name for your Cloud Functions.
- In the Trigger drop-down list, select one of the following
- Cloud Storage
- In the Source code list, select Cloud Source repository.
- In the Repository field, type the name of your repository.
- In the Branch/tag list, select the branch or tag that contains the version of the source code for your function. Then, in the next field, type the name of the branch or tag.
- In the Directory with source code field, type the path to where the function code exists in your repository.
- In the Function to execute field, type the name of the function.
While the function is being deployed, a small spinner is displayed. When the deployment is finished, the spinner becomes a green check mark.
In a terminal window, type the following command. Note that you must modify this command with the Google Cloud project ID, repository ID, and source path.
gcloud functions deploy [FUNCTION_NAME] \ --source https://source.developers.google.com/projects/[PROJECT_ID]/repos/[REPOSITORY_ID]/moveable-aliases/master/paths/[SOURCE] \ --[TRIGGER];
[FUNCTION_NAME]is the name of the function you want to deploy.
[PROJECT_ID]is the ID of your Google Cloud project.
[REPOSITORY_ID]is the name of your repository.
[SOURCE]is the path to your source files.
[TRIGGER]is the type of trigger the Cloud Functions uses, for example,
Test the function
With your function deployed, you can use either the GCP Console or the Cloud SDK to test it.
Go to Cloud Functions Overview page.
Ensure the project for which you enabled Cloud Functions is selected.
Click the name of your function.
The Function details page opens.
Click the Testing tab.
Click Test the function.
From a terminal window, enter the following command:
gcloud functions call [FUNCTION_NAME]
[FUNCTION_NAME] is the name of the function you want to test.
For more information on deploying Cloud Functions to Cloud Source Repositories, see the following Cloud Functions documentation: