GCP Functions
Relevant source files
The following files were used as context for generating this wiki page:
GCP Functions is an Nx plugin within the nx-extend workspace that manages the building and deployment of Google Cloud Functions. It provides tools for local development, automated deployment configurations, and support for various Google Cloud trigger types including HTTP, Pub/Sub, and Cloud Storage.
Sources: packages/gcp-functions/README.md:1-10, packages/gcp-functions/package.json:1-10
Project Initialization
The @nx-extend/gcp-functions:init generator sets up a new Cloud Function project. It configures the project structure, generates necessary source files like main.ts, and adds standard Nx targets for linting, testing, building, and deploying.
Sources: packages/gcp-functions/src/generators/init/init.impl.ts:77-128, e2e/gcp-functions-e2e/tests/gcp-functions.spec.ts:16-24
Initialization Workflow
The diagram shows the sequence of actions performed by the init generator to establish a function project.
Sources: packages/gcp-functions/src/generators/init/init.impl.ts:77-147
Build and Deployment Target Configuration
The plugin defines specific Nx targets in the project.json of each function. The build target uses the @nx-extend/gcp-functions:build executor to compile TypeScript code and optionally generate lock files for deployment. The deploy target uses the @nx-extend/gcp-functions:deploy executor to push the code to Google Cloud Platform.
Sources: packages/gcp-functions/src/generators/init/init.impl.ts:95-125, e2e/gcp-functions-e2e/tests/gcp-functions.spec.ts:26-44
Deployment Options
| Option | Type | Description |
|---|---|---|
functionName | string | Name of the function in GCP. |
trigger | string | Type of trigger: http, bucket, topic, resource, event. |
runtime | string | Node.js runtime version (e.g., nodejs20). |
memory | string | Memory allocation (e.g., 256MB). |
envVarsFile | string | Path to the YAML file containing environment variables. |
secrets | object/array | Secrets to mount into the function. |
gen | number | Cloud Functions generation (1 or 2). |
Sources: packages/gcp-functions/README.md:27-51
Local Development Runner
The init-runner generator creates a local development server that hosts multiple functions simultaneously. This runner uses the bootstrapRunner function to map Nx project names to their entry points, allowing you to test HTTP and event-based triggers locally.
Sources: packages/gcp-functions/src/generators/init-runner/init.impl.ts:60-95, packages/gcp-functions/README.md:73-90
Local Execution Flow
The sequence diagram illustrates how the local runner bootstraps and executes functions during development.
Sources: packages/gcp-functions/README.md:73-90, packages/gcp-functions/src/generators/init-runner/init.impl.ts:80-92
Core Dependencies
The module relies on the Google Cloud Functions Framework and NestJS components for its runner implementation.
- @google-cloud/functions-framework: Provides the runtime environment for executing functions.
- @nestjs/core & @nestjs/platform-express: Powers the underlying server logic for the local runner.
- @nx/devkit: Facilitates integration with the Nx workspace and generator utilities.
Sources: packages/gcp-functions/package.json:13-19, packages/gcp-functions/src/generators/init/init.impl.ts:140-145
Summary
GCP Functions provides a standardized workflow for developing serverless applications within an Nx workspace. By automating project configuration and providing a local runner, it reduces the friction of managing multiple triggers and environment-specific deployments to Google Cloud Platform.