Logo

dev-resources.site

for different kinds of informations.

How to deploy Google Cloud Functions with PNPM workspaces

Published at
11/27/2024
Categories
node
googlecloud
monorepo
serverless
Author
fibonacid
Author
9 person written this
fibonacid
open
How to deploy Google Cloud Functions with PNPM workspaces

Are you struggling to deploy your monorepo project because Cloud Build doesn't know what the heck workspace:* means?
You are in the right place! In this blog I'll show you how to trick mister Google into deploying your PNPM monorepo project.

TL;DR

Just bundle everything, and ship the dist folder to Cloud functions with an empty package.json file. Ta-da! 🎩✨

This is how you do it

Create a simple deploy.sh script inside the package that contains the Cloud Function you want to deploy.

#!/bin/bash

# Install dependencies
pnpm install

# Build the project
npx esbuild \
    ./src/index.ts \
    --bundle \
    --platform=node \
    --target=node20 \
    --outfile=./dist/index.js \
    --external:@google-cloud/functions-framework
Enter fullscreen mode Exit fullscreen mode

In this file we simply install the dependencies with PNPM and bundle the source code with esbuild.
The --external:@google-cloud/functions-framework is required because functions-framework should not be bundled for some reason.
If you have some other dependencies that cannot be bundled, for example a module that uses native bindings, you should append another --external flag for that module.

Now, running the deploy.sh script will create a dist folder with the bundled code.
The only thing we are missing now is a package.json, because Cloud Functions pass through Cloud Build, which has trust issues and wants to build everything by itself.
So we are going to trick it with an empty package.json file.
Put this code right after the esbuild command:

jq -n \
  --arg name "@monorepo/functions" \
  --arg version "1.0.0" \
  --arg main "index.js" \
  --arg start "node index.js" \
  --arg build "echo \"No build step\"" \
  --arg ff_version "^3.3.0" \
  '{
    name: $name,
    version: $version,
    main: $main,
    scripts: {
      start: $start,
      build: $build
    },
    dependencies: {
      "@google-cloud/functions-framework": $ff_version
    }
  }' > ./dist/package.json

Enter fullscreen mode Exit fullscreen mode

We use jq to make the code more readable, but you can also use echo to write the JSON directly.
Now we just need to deploy the dist folder to Cloud Functions.
Add this last line to the deploy.sh script:

gcloud functions deploy myFunction \
    --runtime=nodejs20 \
    --trigger-http \
    --source=./dist
Enter fullscreen mode Exit fullscreen mode

And that's it! Now you can deploy your monorepo project to Cloud Functions with PNPM workspaces.

monorepo Article's
30 articles in total
Favicon
Creating a scalable Monorepo for Vue - Workspaces
Favicon
Creating a scalable Monorepo for Vue - Intro
Favicon
Unlocking the Power of Modern Web Architecture: Monorepos, Micro-Frontends, and Vite 🚀✨
Favicon
Emerging Trends in Git: GitOps, Monorepos, Distributed Repositories, and AI Integration
Favicon
Using Node's built-in test runner with Turborepo
Favicon
Building a Scalable Monorepo with TurboRepo
Favicon
Cookiecutter for fast starting with polylith
Favicon
Monorepo vs Microservices: Finding the Perfect Fit for Your Project
Favicon
Nx + TypeORM + NestJS + Migrations
Favicon
How to Build a Changelog Feature in React for Your App
Favicon
Convert a ReactJS app from Vite to Nx
Favicon
How to deploy Google Cloud Functions with PNPM workspaces
Favicon
How CodeMirror v6 dev setup installs packages without a monorepo
Favicon
How CodeMirror v6 dev setup retrieves packages without a monorepo
Favicon
Nx 20: Exploring the new TS preset and TypeScript project references
Favicon
Simple hello world program using Bazel and Go lang
Favicon
A practical example of shared libraries in a monorepo
Favicon
🗂️ Monorepo vs. Polyrepo: Choosing the Right Strategy for Your Projects 🚀
Favicon
Turborepo vs Nx: Mana yang Terbaik untuk Monorepo?
Favicon
Turborepo vs Nx: Which Monorepo Tool is Right for You?
Favicon
Optimizing +200 Pipelines of a Monorepo
Favicon
GitHub Actions: run a job only if a package has changed
Favicon
Building a Solid Foundation: Bootstrapping with Turbo Repo
Favicon
Nestjs Workspaces to build Monorepo
Favicon
Installing EmberJS v2 addons from GitHub forks using PNPM
Favicon
Understanding Monorepo
Favicon
Build Containerized MERN App with Lerna Monorepo
Favicon
Advanced monorepo management with Turborepo 2.0
Favicon
Vite config reuse
Favicon
Monorepo VS Polyrepo

Featured ones: