Example Cloud Run Function¶
This project demonstrates a Google Cloud Run Function that triggers deployments based on Pub/Sub messages. The function listens for build notifications from Google Cloud Build and initiates a release in Google Cloud Deploy when a build succeeds.
Usage Note¶
This code is designed to integrate with the Terraform configuration for the cloud_deploy_flow demo. While you can deploy this component individually, it's primarily intended to be used as part of the full Terraform-managed workflow. Please note that this section of the README may be less actively maintained, as the preferred deployment method relies on the Terraform setup.
Table of Contents¶
Prerequisites¶
- Go version 1.15 or later
- Google Cloud account
- Google Cloud SDK installed and configured
- Necessary permissions for Cloud Build and Cloud Deploy
Environment Variables¶
The function relies on environment variables to specify project configuration. Ensure these are set before deploying the function:
Variable Name | Description | Required |
---|---|---|
PROJECTID |
Google Cloud project ID | Yes |
LOCATION |
The deployment location (region) | Yes |
PIPELINE |
The name of the delivery pipeline in Cloud Deploy. | Yes |
TRIGGER |
The ID of the build trigger in Cloud Build. | Yes |
SENDTOPICID |
Pub/Sub topic ID for sending commands | Yes |
Function Overview¶
The deployTrigger
function is invoked by Pub/Sub events. Here's a breakdown of
its key components:
-
Initialization:
- Loads environment variables into a configuration struct.
- Registers the function to be triggered by CloudEvents.
-
Message Handling:
- Parses incoming Pub/Sub messages.
- Validates build notifications based on specified criteria (trigger ID and build status).
-
Release Creation:
- Extracts relevant image information from the build notification.
- Constructs a
CreateReleaseRequest
for Cloud Deploy. - Sends the request to the specified Pub/Sub topic.
-
Random ID Generation:
- Generates a unique release ID to ensure each deployment is distinct.
Deploying the Function¶
To deploy the function, follow these steps:
- Ensure that your Google Cloud SDK is authenticated and configured with the correct project.
- Use the following command to deploy the function:
```bash gcloud functions deploy deployTrigger \ --runtime go113 \ --trigger-topic YOUR_TOPIC_NAME \ --env-file .env