Deploy Firebase Apps to Different Environments
Google’s Firebase is a great system and environment to quickly build web apps. However, there are a few tricky things to work around, such as deploying to different environments.
You can use Firebase’s env config variables, but I find them difficult to manage. More than once I used prod variables in dev!
dotenv to the rescue
Here is what I do to create a staging and production environments with different configurations in Firebase.
1. Setup a unique Firebase project for each environment, e.g. Production, Staging, and Development.
2. At a command prompt, assign an alias for each project with:
firebase use --add
3. Create a different .env file for each environment: .env.production, .env.staging, .env.development. Place your specific configs for those environments there.
4. Install “dotenv” with:
npm i --save-dev dotenv
This is assuming your using NodeJS. dotenv is a fantastic way to manage environment configs in node.
5. Add a build script “build-staging” to your package.json file (or build-dev):
"build-staging": "dotenv -e .env.staging react-scripts build"
6. Build with the script:
npm run build-staging
7. Deploy to Firebase:
firebase deploy -P staging
Thank you for reading. 🙏