Skip to content

Instantly share code, notes, and snippets.

@Mearman
Forked from AlissonEnz/functionsDeploy.js
Created June 11, 2021 07:51
Show Gist options
  • Save Mearman/c68ceb6cb4baa44c86defd6f04f2a4f5 to your computer and use it in GitHub Desktop.
Save Mearman/c68ceb6cb4baa44c86defd6f04f2a4f5 to your computer and use it in GitHub Desktop.
Firebase Batch Deploy Functions
// see: https://gist.github.com/AlissonEnz/e4350042a93b7732d3fe9fb138f81b11
// Firebase Batch Functions Deploy (Alisson Enz)
// This script helps firebase users deploy their functions when they have more than 60 functions and
// it's not allowed to deploy all using `firebase deploy --only functions` due deployment quota.
// This script will get your functions export from index.js and deploy in batches of 30 and wait 30 seconds.
// This script will NOT delete your function when removed from index.js.
// Instructions
// 0. This instructions suppose that you already have firebase-tools installed and is logged to your account;
// 1. Install `shelljs` (npm install -g shelljs);
// 2. Change the path to point to your index.js at line 16;
// 3. run `node ./functionsDeploy` to deploy your functions;
import { which, echo, exit, exec } from "shelljs";
import myFunctions from "./lib/index"; // Change here
const project = process.argv[2];
const credentials = process.argv[3];
if (!which("firebase")) {
echo("Sorry, this script requires firebase");
exit(1);
}
const execShellCommand = cmd => {
return new Promise((resolve, reject) => {
exec(cmd, { env: { GOOGLE_APPLICATION_CREDENTIALS: credentials, ...process.env } }, (error, stdout, stderr) => {
if (error) console.warn(error);
resolve(stdout ? stdout : stderr);
});
});
};
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
let count = 0;
let batchesCount = 0;
let batches = [];
const LIMIT = 30;
batches[batchesCount] = "firebase deploy --only";
Object.keys(myFunctions).forEach(f => {
if (count === 0) {
batches[batchesCount] += ` functions:${f}}`;
count += 1;
} else if (count < LIMIT) {
batches[batchesCount] += `,functions:${f}`;
count += 1;
} else {
count = 1;
batchesCount += 1;
batches[batchesCount] = `firebase deploy --only functions:${f}`;
}
});
console.log("\nTotal: ", Object.keys(myFunctions).length);
const save = async () => {
try {
await asyncForEach(batches, async b => {
console.log(`${b} --project=${project}`);
await execShellCommand(`${b} --project=${project} --force`);
await execShellCommand("sleep 30s");
});
} catch (e) {
console.warn(e);
} finally {
console.log("\nDone!\n");
}
};
return save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment