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
- Complete the steps from Create a code repository in Cloud Source Repositories.
- Enable the Cloud Functions API and Cloud Build API.
Add a function to the repository
On your local machine, go to the root directory of the
hello-world
repository:cd hello-world
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
In the
gcf_hello_world
directory, create anindex.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 HTTPGET
requests with the textHello from Cloud Functions and Cloud Source Repositories
.
Push to Cloud Source Repositories
Push the files you just created into Cloud Source Repositories.
Add the files:
git add .
Commit the files with a comment describing the history of this action:
git commit -m "Add Cloud Functions test to Cloud Source Repositories"
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
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.
Click Create function.
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.
Click Save.
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
.
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
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)')
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
Go to the Cloud Functions Overview page.
Make sure that the Google Cloud project for which you enabled Cloud Functions is selected.
Click the Show actions
menu in thecloud-source-repositories-test
function row.Click Test function.
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
Go to the Cloud Functions Overview page.
Make sure that the Google Cloud project for which you enabled Cloud Functions is selected.
Select the
helloGET
function for this quickstart,cloud-source-repositories-test
.On the same row, click More more_vert, and then click Delete.
Delete the repository
In the GCP Console, open the All repositories page for Cloud Source Repositories.
Hold the pointer over the repository you want to delete and click Settings settings.
The General settings page opens.
Click Delete this repository delete.
The Remove repository dialog opens.
Type the name of the repository you want to delete.
Click Delete.
What's next
- Learn more about Cloud Functions.