simondotm / nx-firebase

Firebase plugin for Nx Monorepos
https://www.npmjs.com/package/@simondotm/nx-firebase
MIT License
175 stars 31 forks source link

Deployment Environments usage #181

Closed Lowell20 closed 3 months ago

Lowell20 commented 6 months ago

Am trying to deploy an app to either a prod or a dev environment (two separate firebase projects) From the docs, this explanation almost gets me there, but I think I need a clarification

You can now run:

nx deploy my-firebase-app for dev deployment nx deploy my-firebase-app --prod for production deployment Note that your-dev-firebase-project and your-prod-firebase-project must exist as aliases in your .firebaserc file for this to work.

Would this be 2 different nx web apps? Or would the same web app get deployed to prod-project with --prod and dev-project without --prod

Based on the same app deploying to different firebase projects, I would think .firebaserc should look like this, but of course this can't work.

  "projects": {
    "default": "",
    "nx-project-name": "your-prod-firebase-project ",
    "nx-project-name": "your-dev-firebase-project"
  },
  "targets": {},
  "etags": {}
}
Lowell20 commented 6 months ago

Using the --prod flag but not deploying to the "production" configuration nx deploy my-firebase-app-firebase --prod and in my-firebase-app/firebase/project.json

      "executor": "nx:run-commands",
      "options": {
        "command": "firebase --config=firebase.my-firebase-app-firebase.json --project=firebase-dev-project"
      },
      "configurations": {
        "production": {
          "command": "firebase --config=firebase.my-firebase-app-firebase.json --project=firebase-prod-project"
        }
      }
    },
simondotm commented 6 months ago

When you have a dev and prod environment setup, you will have to create two firebase projects in your firebase account console - lets call them your-prod-firebase-project and your-dev-firebase-project

You then need to add these to your .firebaserc config with aliased names (unrelated to your Nx project names)

  "projects": {
    "default": "dev",
    "prod": "your-prod-firebase-project ",
    "dev": "your-dev-firebase-project"
  },

You only need one nx-firebase project in your Nx workspace, because we're saying we'll want to deploy this nx-firebase project to either prod or dev depending on the --prod configuration.

So your config above is correct (I've just renamed to use the example aliases here):

      "executor": "nx:run-commands",
      "options": {
        "command": "firebase --config=firebase.my-firebase-app-firebase.json --project=dev"
      },
      "configurations": {
        "production": {
          "command": "firebase --config=firebase.my-firebase-app-firebase.json --project=prod"
        }
      }
    },

Now, when we run deploy with --prod the Firebase CLI command will use --project=prod, so will deploy to your prod firebase project, and without --prod it will deploy the same Nx project to your dev firebase project instead (due to --project=dev being passed to firebase cli).

See here for more info

Lowell20 commented 6 months ago

Thanks for the feedback, seem to be getting further along with this but am still running into issues.

*note: found typo, missing ", so now can run without errors.

running nx deploy nx-project-firebase --prod it seems to be using dev instead of prod

am using dev and prod as aliases as shown here:

{
  "projects": {
    "default": "",
    "prod": "prod-firebase-project",
    "dev": "dev-firebase-project"
  }
}

and here:

    "firebase": {
      "executor": "nx:run-commands",
      "options": {
        "command": "firebase --config=firebase.nx-project-firebase.json --project=dev"
      },
      "configurations": {
        "production": {
          "command": "firebase --config=firebase.nx-project-firebase.json --project=prod"
        }
      }
    }
Lowell20 commented 3 months ago

Solved, documenting and closing this issue,

Was able to add configuration options to the deploy target that would then call the firebase target with the needed configuration option, both of these targets are in the app/projectName/firebase/project.json file. For some reason, probably my lack of understanding, .firebaserc did not seem to be a factor at all.

{
  "name": "projectName-firebase",
  "$schema": "../../../node_modules/nx/schemas/project-schema.json",
  "projectType": "application",
  "targets": {
    "deploy": {
      "executor": "nx:run-commands",
      "dependsOn": [
        "build"
      ],
      "options": {
        "command": "nx run projectName-firebase:firebase deploy"
      },
      "configurations": {
        "production": {
          "command": "nx run projectName-firebase:firebase:production deploy"
        },
        "development": {
          "command": "nx run projectName-firebase:firebase:development deploy"
        }
      }
    },
   "firebase": {
      "executor": "nx:run-commands",
      "options": {
        "command": "firebase --config=firebase.projectName-firebase.json --project=development_firebaseProjectName"
      },
      "configurations": {
        "production": {
          "command": "firebase --config=firebase.projectName-firebase.json --project=production_firebaseProjectName"
        },
        "development": {
          "command": "firebase --config=firebase.projectName-firebase.json --project=development_firebaseProjectName"
        }
      }
    }
  }
}