-
-
Save fatihacet/97fbd7c514f617dd1da303895edd8093 to your computer and use it in GitHub Desktop.
Retry failed Cloud Function deployments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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