Cloud Deploy Operations Function¶
This project contains a Google Cloud Run Function written in Go, designed to interact with Google Cloud Deploy. The function listens for deployment events on a Pub/Sub topic, processes those events, and triggers specific deployment operations based on the event details. For instance, when a deployment release succeeds, it triggers a rollout creation and sends the relevant command to another Pub/Sub topic.
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.
Requirements¶
- Go 1.20 or later
- Google Cloud SDK
- Google Cloud Pub/Sub
- Google Cloud Deploy API
- Set environment variables for Google Cloud project configuration
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 |
SENDTOPICID |
Pub/Sub topic ID for sending commands | Yes |
Structure¶
Main Components¶
- config: Stores the environment configuration necessary for the function.
- PubsubMessage: Structure representing a message from Pub/Sub, with
Data
payload andAttributes
metadata. - OperationsData: Metadata that describes deployment action and resource details.
- CommandMessage: Structure for deployment commands, like
CreateRollout
. - cloudDeployOperations: Main Cloud Run Function triggered by a deployment event, processes release successes to initiate rollouts.
- sendCommandPubSub: Publishes a
CommandMessage
to a specified Pub/Sub topic, which triggers deployment operations.
Function Workflow¶
- Trigger: The function
cloudDeployOperations
is triggered by a deployment event, specifically a CloudEvent. - Event Parsing: The function parses the event data into a
Message
struct, checking for deployment success events. - Rollout Creation: If a release success is detected, it creates a
CommandMessage
for a rollout and callssendCommandPubSub
. - Command Publish: The
sendCommandPubSub
function publishes theCommandMessage
to a designated Pub/Sub topic to initiate the rollout.
Setup and Deployment¶
Local Development¶
- Clone the repository and set up your local environment with the necessary environment variables.
- Run the Cloud Run Functions framework locally to test the function:
Deployment to Google Cloud Run Functions¶
-
Set up your Google Cloud environment and enable the necessary APIs:
-
Deploy the function to Google Cloud:
Error Handling¶
- If message parsing fails, the function logs an error but acknowledges the message to prevent retries.
- Command failures are logged, and the function acknowledges the message to prevent reprocessing of erroneous commands.
License¶
This project is licensed under the MIT License. See the LICENSE
file for details.
Notes¶
- For production environments, consider validating that the
TargetId
withinCommandMessage
is dynamically populated based on actual Pub/Sub message data. - The function relies on
pubsub.NewClient
which should be carefully monitored in production for connection management.