A node.js CLI that allows users to deploy multiple Google Cloud functions (in separate folders) using a single command.
A while back, I migrated several cloud functions from Netlify to the Google Cloud platform and noticed that the Google Cloud CLI (gcloud
command) requires that developers execute a separate deployment command for each function deployment.
My project had three functions and I wanted a way to deploy all three with a single command. In Google Cloud Multiple Functions in a Project I described how I added code to my project that allowed me to easily do this using Google zx as an execution helper.
In an effort to make that code easier to use across multiple project, I decided to make a node-based CLI that allows me to easily execute batch deploying across multiple Google Cloud Functions projects. This version of the code uses Execa to manage spawning external processes.
To use this module, install it globally then add a simple (automatically generated by the module) configuration file to the project folder. With the correct configuration in place, you can deploy all cloud functions in a project folder using the single gfdeploy
command.
To install the command, open a terminal window, then execute the following command:
npm install -g google-functions-deploy
Note: You should not need to use sudo
on linux or macOS to install the module. If you find that you need to or want to, instead go back and fix permissions for your local node.js installation and try it again.
The module adds a gfdeploy
command you can use in any Google Cloud Functions project to deploy a function or group of functions in a single command - without requiring any command-line parameters.
The module requires a configuration file, so on first run it will offer to create it for you. To configure the module, open a terminal window in the Google Cloud Functions project then execute the following command:
gfdeploy
Here's an example of automated configuration console output:
┌──────────────────────────────┐
│ │
│ Deploy Google Functions │
│ │
└──────────────────────────────┘
by John M. Wargo (https://johnwargo.com)
Configuration path: D:\dev\node\google-functions-deploy\gfdeploy.json
Configuration file missing:
Rather than requiring the use of a bunch of command-line arguments, this tool uses a configuration file instead.
In the next step, the module will automatically create the configuration file for you.
Once it completes, you can edit the configuration file to change the default values and execute the command again.
√ Create configuration file? ... yes
√ Use default flags? ... yes
√ Select one or more function folders to deploy: » Function1, Function2, Function3
Writing configuration file D:\dev\node\google-functions-deploy\gfdeploy.json
Output file written successfully
Open D:\dev\node\google-functions-deploy\gfdeploy.json in an editor to modify configuration settings.
If you run the command in a terminal window inside Visual Studio Code, the module will open the file in the editor after creating the file.
You can also skip that process altogether and create the file manually. To do that, create a file in the project root called gfdeploy.json
and populate it with two JSON arrays: functionFolders
and flags
as shown in the example below:
{
"functionFolders": [],
"flags": []
}
The following table describes each configuration option:
Configuration Array | Description |
---|---|
functionFolders |
The array of sub-folders that contain Google Cloud Functions functions. Each folder represents a single function. |
flags |
The array of gcloud functions deploy command-line arguments required to successfully deploy the function to Google's cloud platform. The module will automatically populate the function name in the command. |
Here's an example of a configuration file populated with configuration information for three functions (Function1, Function2, and Function3) deployed to us-east1
using node.js version 20 with an HTTP trigger allowing unauthenticated access:
{
"functionFolders": [
"Function1",
"Function2",
"Function3"
],
"flags": [
"--region=us-east1",
"--runtime=nodejs20",
"--trigger-http",
"--allow-unauthenticated"
]
}
Note: The options in the flags
array shown in the previous example are the default flags used when you answer Yes
to the "Use default flags?" prompt.
The gcloud functions deploy
command supports a wide selection of command-line options, refer to the product documentation for information about all of the available options. Rather than code support for every command-line option, all you have to do is populate the flags
array with the appropriate settings for your particular project and the module takes care of passing them to the gcloud
command.
With a populated configuration in place, to deploy all of the functions in a project to Google Cloud Functions, open a terminal window in the project folder and execute the following command:
gfdeploy
At this point, the module loops through all the folders listed in the functionsFolders
array, and executes the deployment command for each, writing all output to the console as it completes the task.
If this code helps you, please consider buying me a coffee.