A template for creating Python functions on the Google Cloud Functions (Gen2) platform with CI/CD integration using GitHub Actions.
This project provides a ready-to-use structure for developing and deploying Python functions on Google Cloud Functions Gen2. It includes continuous integration and delivery setup with GitHub Actions, tools for testing, and dependency management.
I am an entrepreneur and researcher passionate about creating new products, testing hypotheses, and automating business processes. My core belief is that hypotheses should be tested quickly and with minimal cost. That's why I use serverless functionality, which can be deployed instantly. With platforms like Google Cloud's pay-as-you-go model, projects with low usage do not incur unnecessary expenses.
To ensure scalability, maintainability, and ease of development for successful projects, I incorporate quality control tools such as type checkers, linters, tests, and code coverage tools. These minimize errors and improve code quality.
I have also set up a CI/CD pipeline, enabling fast team collaboration and quick production deployments. This ensures efficient development cycles.
In this project, I implemented two types of handlers and three types of event issuers:
- HTTP request handlers – For handling synchronous HTTP requests.
- Queue event handlers – For processing asynchronous messages from a queue.
I prefer a declarative approach, and the entire infrastructure for this project is described using Terraform manifests.
I use diagrams to visualize the project's architecture and data flow. These diagrams are stored in the docs directory and can be generated using the make diagrams command.
Create diagrams in the https://mermaid.live/ editor and then copy the generated code to the corresponding .mmd file.
Pre-commit hook automaticaly detects changes in the .mmd files and generates .svg files.
I prefer to store this generated SVGs in the README to easily understand the project structure. It helps a lot when you have a lot of services, and you need to understand the data flow quickly. Take a look on the section Diagrams for more details.
- HTTP Webhooks – For triggering events based on external sources.
- Queue publishing – For sending messages to a queue for further processing by Google Cloud Functions.
- Scheduled publishing – For recurring tasks, such as executing small, repetitive jobs.
To demonstrate this template's capabilities, I implemented the following use case:
-
Scheduled Events:
- Google Cloud Scheduler sends an event to a queue every hour.
-
Queue Processing:
- A Google Cloud Function processes the event from the queue and passes it through a series of handlers.
- Currently, one handler is implemented:
CheckSiteForUpdate. This handler checks if specific keywords on a given website (or websites) have changed. If changes are detected, it sends a webhook notification.
-
Notification Example:
- In this implementation, the webhook sends a notification to a Telegram Bot, which forwards the alert directly to my Telegram account. (The Telegram interaction implemented in a third-party service)
-
Manual Trigger:
- An HTTP request handler is also implemented (used by the same Telegram Bot) to send a message to the queue, triggering an out-of-schedule website update check.
-
Install the required tools:
-
Clone the repository:
git clone https://github.com/CthtufsPetProjects/cloud-function-gen2-template.git
-
Navigate to the project directory:
cd cloud-function-gen2-template -
Install dependencies:
poetry install
-
Init development:
It would create .env file and docker-compose configuration from templates and install pre-commit hooks.
make init_development
To enable CI/CD pipelines, follow these steps:
-
Add the following secrets to your GitHub Actions:
- GCP_PROJECT_ID – Your Google Cloud project ID.
- GCP_REGION – The Google Cloud region where the functions will be deployed.
- GCP_SA_KEY – Service Account key JSON (Get content for this secret from file config/ghsa.json).
-
For test coverage metrics:
- Register your project on Codecov.
- Obtain the CODECOV_TOKEN for your project and add it to GitHub secrets.
-
Create Google Cloud project and get Project ID.
-
Fill variables in the
terraform.tfvarsfile.cp terraform.tfvars.template terraform.tfvars
-
Deploy the infrastructure:
terraform -chdir=infra init terraform -chdir=infra apply
-
Setup CI (see the CI Setup section).
-
Commit changes and push to your repository.
Note: I suggest to keep terraform state in the repository for simplicity. For production, use a remote state.
-
Create PR and check PR status.
-
Merge PR to the
mainbranch. -
Check the GitHub Actions tab for the CI/CD pipeline status.
Community contributions warmly welcomed! Please create pull requests or open issues to discuss suggestions and improvements.
This project is distributed under the MIT License. See the LICENSE file for details.