I’ve implemented two firebase projects; production (default) and staging
In my Vue3 project.. I have the following env files:
.env.local, .env.production, and .env.staging
npm run dev //uses .env-local. works fine
firebase deploy -P default //uses .env-production. works fine
HOWEVER.. running:
firebase deploy -P staging //uses .env-production. !!!WRONG!!!
My vite.config.js file:
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
Example .env file:
VITE_ENV = "development"
VITE_FIREBASE_API_KEY = "sdasdasdsada"
VITE_FIREBASE_AUTH_DOMAIN = "sadadasdasd.firebaseapp.com"
VITE_FIREBASE_PROJECT_ID = "sadasdasd"
VITE_FIREBASE_STORAGE_BUCKET = "asdasdas"
...
QUESTION:
How do I get firebase deploy -P staging
to use the .env.staging environment?
2
Answers
You might consider adding a new script for staging deployment in your
package.json
: that script will set the appropriate environment variables before deploying to Firebase.cross-env
(npm install --save-dev cross-env
) is used to set environment variables in a platform-independent way. It is in maintenance mode as a project, but still works well.Your
package.json
should now include the new deployment script:The provided
vite.config.js
does not currently handle custom environment variables. You would need to modify it to make sure it loads the correct environment variables based on theVITE_ENV
value.That would load the appropriate
.env
file based on themode
(which is set bycross-env VITE_ENV=staging
or other environment settings).The example
.env
file you mention suggests that these are standard dotenv files.However, to be imported in
vite.config.js
, they need to be in JavaScript object format. Ypu might consider converting the.env
files into JavaScript modules. For instance, consider usingabdennour/dotenv-to-js-object
(Dockerfile, using this bash scriptfry
) to convert the.env
files into JavaScript modules, which would look like.Repeat this for each environment (
.env.production.js
,.env.local.js
, etc.).That could be part of your npm script, to include this conversion step. For instance:
But you could use the
fry
script directly, without Docker:That assumes your
.env.staging
file is in the root directory of your project. Theconvert-env
script will create a.env.staging.js
file, which can then be imported into yourvite.config.js
.That would automate the process of converting
.env
files.Now, when you want to deploy to your staging environment, you would use the new
npm
script:That should make sure the correct environment variables are used for each deployment type.
Make sure the
.env
files are correctly named (.env.local
,.env.production
,.env.staging
) and contain the respective environment-specific variables.You want to make sure you initialize the correct project before deploying.
Write a script that will initialize your secondary firebase project before deploying your code to it. Then add it to package.json.
inside package.json
rough script