Skip to content

Setup automated builds and deployments for Cloud Functions using Cloud Build

Notifications You must be signed in to change notification settings

garrettmmoore/gcloud-functions-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automate Deployments for Google Cloud Functions

Setup automated builds and deployments for Cloud Functions using Cloud Build

Requirements

  • Node.js
  • Google Cloud Account
  • Google Cloud SDK Command Line Tools

Set up Local Development Environment

Connect your repository to Cloud Build

  • Fork this repository
  • Clone your forked version of this repository
  • Navigate to Google Cloud Build/Triggers to connect your GitHub repository
  • Select the GitHub "Cloud Build GitHub App" option

alt text

  • Provide access to your source repository
  • Create a push trigger

Enable Google Cloud API's

In Cloud Build settings, enable Cloud Functions and Service Accounts

alt text

In API's & Services, enable the following API's for your project:

  • Cloud Functions API (For deploying cloud functions)
  • Cloud Build API (For automated builds)
  • Cloud Logging API (For build logs)
  • Cloud Pub/Sub API (For Pub/Sub test)
  • Cloud Resource Manager API (Step 2 will fail without this enabled)

Prepare for running tests

Create a storage bucket

Create a Pub/Sub topic

Set the following environment variables:

export $FUNCTIONS_TOPIC=<YOUR_TOPIC_NAME>
export $FUNCTIONS_DELETABLE_BUCKET=<YOUR_BUCKET_NAME>

Manually deploy the following cloud functions:

helloGET -> `gcloud functions deploy helloGET --runtime nodejs12 --trigger-http`

helloHTTP -> `gcloud functions deploy helloHttp --runtime nodejs12 --trigger-http`

helloPubSub -> `gcloud functions deploy helloPubSub --trigger-topic $FUNCTIONS_TOPIC --runtime nodejs12`

helloGCS -> `gcloud functions deploy helloGCS --runtime nodejs12 --trigger-resource $FUNCTIONS_DELETABLE_BUCKET --trigger-event providers/cloud.storage/eventTypes/object.change`

Run the tests

From the helloWorld directory, install the project dependencies by running the following command:

npm install

Then, run the tests:

npm run test

Run the build

Make a commit and push to the repository. This will trigger the build to begin. Google Cloud Build will read the cloudbuild.yaml file to build and deploy the cloud function. Navigate to your Cloud Build Dashboard History to see your build in progress.

A successful build should look like this:

alt text

References

Note - The functions and test used in this repository is from the GoogleCloudPlatform nodejs-docs-samples examples.

About

Setup automated builds and deployments for Cloud Functions using Cloud Build

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published