Deploy multiple environments from a codebase

It's common to have multiple environments deployed from the same codebase, each with slightly different configuration. For example, you might want to assign less CPU and RAM to your staging environment, or you might want to make sure your production environment keeps at least 1 instance active and ready to serve requests.

To learn more about environments, check out the Overview of environments and General best practices for setting up Firebase projects.

Prerequisites

Step 0: Set up your production git branch

If you prefer to deploy your production environment from your main branch, skip ahead to the next step. If you want to deploy production from another branch such as. production, you must first create it on GitHub.

To create your production branch with the GitHub UI, see Creating and deleting branches within your repository.

To create your production branch locally:

$ git checkout -b production
$ git push origin production:production

Step 1: Configure your production apphosting.yaml

Let's say you want to make sure your production always keeps at least 1 machine running and has some production-specific environment variables. You would configure that by creating an apphosting.yaml file at the root of your project with something like the following:

# Saved at <repository root>/apphosting.yaml

runConfig:
  minInstances: 1

env:
  - variable: STORAGE_BUCKET
    value: <production Cloud Storage bucket name>

To see a full list of supported settings, see Configure App Hosting.

After editing apphosting.yaml, push it to your GitHub repository:

$ git add apphosting.yaml
$ git commit -m "Added production App Hosting backend configuration."
$ git push origin

Step 2: Deploy your codebase to production

Once your production git branch is configured, you can create your new production App Hosting backend. For detailed instructions, see Get started with App Hosting.

To create a new backend with the Firebase Console, navigate to https://console.firebase.google.com/project/_/apphosting to get started.

To create it locally, you must first have the Firebase CLI installed, and then run:

firebase apphosting:backends:create --project <replace with your PRODUCTION project ID>

When this command succeeds your production backend should be live, and any new commits to your production branch will automatically be built and deployed by App Hosting.

Step 3: Set up your staging git branch

Follow the same steps as above to create your staging branch on GitHub:

$ git checkout -b staging
$ git push origin staging:staging

Step 4: Configure your staging apphosting.yaml

Your staging branch should already have a copy of your production apphosting.yaml. Let's modify it so App Hosting can turn down your staging environment when it's not actively being used, and also set a different value for the STORAGE_BUCKET environment variable.

# Saved at <repository root>/apphosting.yaml

runConfig:
  minInstances: 0

env:
  - variable: STORAGE_BUCKET
    value: <some other staging Cloud Storage bucket name>

After finishing your edits, push the file to your staging git branch:

$ git add apphosting.yaml
$ git commit -m "Added staging App Hosting backend configuration."
$ git push origin

Step 5: Deploy your codebase to staging

Once your staging git branch is configured, you can follow the same steps as above to deploy your codebase to staging:

firebase apphosting:backends:create --project <replace with your STAGING project ID>

After completing this step you'll have two App Hosting backends powered by the same codebase, one for production and one for staging, each in a different Firebase project.

Next steps