I'm moving a project to a monorepo (Turborepo w/pnpm) and having trouble deploying my Google Cloud Functions. Normally I would simply use the gcloud CLI. For example:
call pnpm run build && gcloud functions deploy myCloudFunction \\
--entry-point myMainFunction --env-vars-file .env.yaml \\
--runtime nodejs16 --trigger-topic MY_TOPIC
However now I have the obvious problem - in the monorepo I have local dependencies referencing "workspace:*" so the "gcloud functions deploy" build process fails. Is there an elegant way to build and package my code so that "gcloud functions deploy" will have access to all the local dependencies it needs?
In case anyone else is looking for the solution, the easiest way to make this work seems to be:
pnpm pack
The zipped project might look like this. ".npmrc" can be used if you have private npm packages to include. The "dist" folder just has the "index.js" build by the pnpm packer.
The package.json file might look like this:
{
"name": "my-cloud-function",
"version": "0.0.1",
"private": true,
"description": "handle various Pub/Sub messages",
"main": "dist/index.js",
"files": [
"dist",
".npmrc"
],
"dependencies": {
[... your dependencies]
},
"license": "UNLICENSED",
"devDependencies": {
"@google-cloud/functions-framework": "^3.2.0",
"@types/express": "^4.17.11",
"@types/node": "^18.11.6"
}
}
gsutil cp myfunction.zip gs://some-bucket/build
gcloud functions deploy myCloudFunction \\
--source gs://some-bucket/myfunction.zip \\
--entry-point myMainFunction --env-vars-file .env.yaml \\
--runtime nodejs16 --trigger-topic MY_TOPIC
That does it. Using "pnpm pack" automatically deals with packing up the local dependencies that are referenced as "workspace:*" in the monorepo.