Google Cloud Functions Tutorial : Deploying from a Source Repository

Romin Irani
Romin Irani’s Blog
5 min readOct 23, 2018

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

In the series so far, we have deployed our source code directly. What I mean by that is that we either had the source code present in a local folder and then we used gcloud to deploy the function or we used the Cloud Console Web UI to paste our source code directly. If you have taken a closer look, you would have noticed that one of the options available for deploying your Function is to use a Source Code Repository like Git to host your function code and deploy it directly from there.

The ability to host our Source code in a Git repository and use that as a basis to deploy our function code has multiple benefits. It fits with a typical development workflow that we use today. Additionally, you can maintain versions of your function code and also have automatic builds done for you via the source code checkin trigger. Let’s learn the mechanics of how to do this now.

First up, if you visit the Cloud Console Web UI for creating a Cloud Function, you can see the option for Source Code Repository as shown below (only relevant sections from the Create Function form ):

Google Cloud Source Repositories

Did you know that each Google Cloud Platform project comes with a Git repository ? This is available in the Cloud Console main menu via the Source Repositories option shown below:

What we shall be doing is the following:

  1. As mentioned earlier, all the code in this series is available at Github at https://github.com/rominirani/googlecloudfunctions-training. So we already have a Git repository created for us over there. I am assuming that you have cloned this repository under our own Github account. This is important and do not miss this step.
  2. We are going to initialize a repository in our GCP Project and connect it to the Github repository that we have created in Step 1. All code that you push / update to this Github repository will be kept in sync by the Google Cloud Source Repository.
  3. At the time of deploying our function, we can refer to the Google Cloud Source Repository, specify the branch and the folder in which the specific function source is present and the deployment then happens automatically for us.

Let’s do it.

Google Cloud Source Repositories has been revamped significantly and a Beta version is available, at the time of writing this blog post. I shall use the Beta version and in case you see a message asking you to use the Beta version, please do so.

The Beta version available at https://source.cloud.google.com/ looks like this:

If you click on View All Repositories, you will see all your GCP Projects and the associated repositories for the same. We shall go ahead and create a new respository. To do that, click on the New repository button in the top right corner.

You have a couple of options as shown below and I am choosing the 2nd option since I would like to connect this repository to an already initialized and available repository that I have for this series at Github.

Click on Continue. This will bring up a screen below where you should select your existing GCP Project and choose the provider as Github. Give the appropriate consent and complete the formalities of connecting to Github.

Once you click on Connect to Github, you will need to authorize Google Cloud Platform to access your Github account and repositories that you have under it and select the particular repository on Github to connect to. Please do that. You should see a message similar to the following:

If all goes well, your Github repository will be available under Source Repositories as shown below:

We are able to see our README.md file and that is a good thing ! :-)

This completes our section of connecting an existing Github repository to Cloud Source Repositories. You can continue to use your standard Git workflow to checkin files into Github repository and the same will get mirrored into the Cloud Source Repositories. The synchronisation is done for you.

The important thing to note from the above screen is the repository name and the branch name. An example is shown below. This should also help you understand that a GCP Project can have one or more repositories (a default one and multiple others like these as shown below).

You can click on the Repository and Branch drop down arrows to view the details. What I particularly liked was that you could look at various branches, tags, commits and everything that has happened in Github directly from Cloud Source Repositories.

Deploying our function from Cloud Source Repository

We are going to deploy a HTTP Trigger based function that is present in the helloworld-http folder of our repository as shown below:

Few things to note here:

  1. Repository Name : github_rominirani_googlecloudfunctions-training
  2. Branch/Tag : Branch
  3. Branch Name : master
  4. Folder in which the index.js file is present : helloworld-http
  5. Function to execute : helloGET

Once ,we have this information, we can visit the GCP Console for New Function and provide the information as given above and click on Create Function. Your function should deploy successfully and ready to be invoked!

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

--

--

Romin Irani
Romin Irani’s Blog

My passion is to help developers succeed. ¯\_(ツ)_/¯