Skip to content

Instantly share code, notes, and snippets.

@fatihacet
Created April 29, 2020 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatihacet/97fbd7c514f617dd1da303895edd8093 to your computer and use it in GitHub Desktop.
Save fatihacet/97fbd7c514f617dd1da303895edd8093 to your computer and use it in GitHub Desktop.
Retry failed Cloud Function deployments
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
(async () => {
const content = await fs.readFileSync(path.join(__dirname, '../index.js'), { encoding: 'UTF-8' });
const REGEX = /exports.(\w+) = require/gm;
const fnNames = content.split('\n').reduce((acc, line) => {
const match = REGEX.exec(line);
if (match) {
acc.push(`functions:${match[1]}`);
}
return acc;
}, []);
let failedDeployCommand = '';
const deploy = (fns) => {
failedDeployCommand = '';
const firebaseCommand = spawn('firebase', ['deploy', '--token', process.env.FIREBASE_CLI_TOKEN, '--only', fns]);
firebaseCommand.stdout.on('data', (data) => {
if (data.includes('firebase deploy --only')) {
failedDeployCommand = data.toString();
}
console.log(data.toString());
});
firebaseCommand.stderr.on('data', (data) => {
console.log(data.toString());
});
firebaseCommand.on('close', () => {
if (failedDeployCommand) {
console.log('FailedDeployCommand :>> ', failedDeployCommand);
const regex = /functions:(\w+)/gm;
const failedFunctions = failedDeployCommand.match(regex);
console.log('Retrying deployment for: ', failedFunctions.join(','));
deploy(failedFunctions.join(','));
} else {
console.log('There are no failed functions');
}
});
firebaseCommand.on('error', (err) => {
console.log('Cloud Function deployment failed.', err.toString());
process.exit(1);
});
};
deploy(fnNames.join(','));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment