Setup automated builds and deployments for Cloud Functions using Cloud Build
- Node.js
- Google Cloud Account
- Google Cloud SDK Command Line Tools
- Install Node.js ^12.0.0
- Create a Google Cloud Account
- Create a new project
- Install the Google Cloud SDK Command Line Tools
- Run
gcloud init
in your terminal to connect your Google Cloud Account
- 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
- Provide access to your source repository
- Create a push trigger
In Cloud Build settings, enable Cloud Functions and Service Accounts
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)
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`
From the helloWorld
directory, install the project dependencies by running the following command:
npm install
Then, run the tests:
npm run test
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:
Note - The functions and test used in this repository is from the GoogleCloudPlatform nodejs-docs-samples examples.