How To Deploy Google Cloud Functions from CircleCI

Create A Service Account and Keys

To set up automated deployment with CircleCI, we first need to create a service account. This is basically a public/private key-pair that will allow CircleCI to login and perform actions on your Google Cloud project; in this case, we’re restricting the actions to Cloud Functions.

Login to your console and select “Service Accounts” from the IAM & admin menu.
Click the “CREATE SERVICE ACCOUNT” button to create a service account.
Click the create button to create the service account — you’ll be taken to the next screen in the wizard.
Assign the “Cloud Functions Developer” and “Service Account User” roles, so the service account can perform deployment actions.

Enable The Cloud Functions API

If it’s not already enabled, you’ll need to enable the Cloud Functions API by visiting the Google Developer Console and clicking enable.

Create the CircleCI Project and Add Environment Variables

Now that we’ve got our private key, we need to create a CircleCI project.

An example .circleci/config.yaml file for deploying the src directory to Google Cloud Functions.

Setting Up The CircleCI Project

If the project has yet to be set up on CircleCI we will need to do that. Visit the CircleCI Dashboard and click “ADD PROJECTS”. Choose your project from the list and click the setup button.

Set Environment Variables

Remember that service_key.json file we downloaded? We now need to store it as a project environment variable in CircleCI so it can be injected into the container when it’s time to deploy.

cat the_service_account_file.json | base64
Don’t share this key with anyone — even the base64 encoded version. (This key can be shared because it has already been destroyed).

Deployment

With our current CircleCI configuration, any time a change (or merge) is made to master, a new version of our cloud function will be deployed automatically.

A completed deployment from the CircleCI console.
The deployed cloud function.

Questions?

Have any questions? Leave a comment!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryder Damen

Ryder Damen

Ryder Damen is a DevOps engineer by day, and also a DevOps engineer by night. He enjoys travelling, trying new things and making the web a stranger place.