Deploy targets are short-name identifiers (that you define yourself) for Firebase resources in your Firebase project, like a Hosting site with unique static assets or a group of Realtime Database instances that share the same security rules.
Deploy targets are useful when you have multiple Hosting sites, multiple Cloud Storage buckets, or multiple Realtime Database instances. With deploy targets, the Firebase CLI can deploy settings to a specific Firebase resource or group of resources in your project, such as:
- The hosting configuration for each of your Hosting sites
- Static assets from your project directory for each of your Hosting sites
- Security rules shared by multiple Realtime Database instances or multiple Cloud Storage buckets
To set up a deploy target:
- Apply a
TARGET_NAME
to the targeted Firebase resource or group of Firebase resources. - In your
firebase.json
file, reference the associatedTARGET_NAME
when you're configuring the settings for each resource or group of resources.
When you run Firebase CLI commands (like
firebase deploy
), the Firebase CLI pairs each TARGET_NAME
with its
associated Firebase resources. The CLI then communicates to your Firebase
project the settings for each resource.
Set up deploy targets for your Firebase resources
Using the Firebase CLI, apply a TARGET_NAME
(short-name identifier that
you define yourself) to a Firebase resource or group of Firebase resources.
Firebase supports deploy targets for:
- Firebase Hosting sites
- Cloud Storage for Firebase storage buckets
- Firebase Realtime Database instances
The settings for deploy targets are stored in the .firebaserc
file in your
project directory, so you only need to set up deploy targets one time per
project.
Set up deploy targets for Hosting
To create a deploy target and apply a TARGET_NAME
to a Hosting site, run
the following CLI command:
firebase target:apply TYPE TARGET_NAME RESOURCE_IDENTIFIER
Where the parameters are:
TYPE — the relevant Firebase resource type
- For Firebase Hosting sites, use
hosting
.
- For Firebase Hosting sites, use
TARGET_NAME — a unique name for the Hosting site that you're deploying to
RESOURCE_IDENTIFIER — the
SITE_ID
for the Hosting site as listed in your Firebase project
For example, if you've created two sites
(myapp-blog
and myapp-app
) in your Firebase project, you could apply a
unique TARGET_NAME
(blog
and app
, respectively) to each site by running
the following commands:
firebase target:apply hosting blog myapp-blog
firebase target:apply hosting app myapp-app
Set up deploy targets for Cloud Storage or Realtime Database
To create a deploy target and apply a TARGET_NAME
to a set of
Cloud Storage or Realtime Database resources, run the following CLI
command:
firebase target:apply TYPE TARGET_NAME RESOURCE-1_IDENTIFIER RESOURCE-2_IDENTIFIER ...
Where the parameters are:
TYPE — the relevant Firebase resource type
- For Cloud Storage buckets, use
storage
. - For Realtime Database instances, use
database
.
- For Cloud Storage buckets, use
TARGET_NAME — a unique name for the resource or group of resources that share security rules
RESOURCE_IDENTIFIER — the identifiers for the resources as listed in your Firebase project (like storage bucket names or database instance IDs) that all share the same security rules
For example, you could apply the TARGET_NAME
of main
to a group of three
regional Cloud Storage buckets (that all share the same security rules)
by running the following command:
firebase target:apply storage main myproject.firebasestorage.app myproject-eu myproject-ja
Note that myproject.firebasestorage.app
is the identifier for the
default bucket, while myproject-eu
and myproject-ja
are two additional
buckets created in the Firebase project.
Configure your firebase.json file to use deploy targets
After you've set up deploy targets for your Firebase resources, reference each
applied TARGET_NAME
in your
firebase.json
configuration file:
- Create an array of configuration objects for each Firebase resource
TYPE
(hosting
,storage
, ordatabase
). - In the arrays, specify the
target
(using theTARGET_NAME
) and define your settings for the associated Firebase resource or group of resources.
Continuing the examples from above, where your Firebase project has two
Hosting sites and three Cloud Storage buckets (that share the same
security rules), your firebase.json
file would look like this:
{ "hosting": [ { "target": "blog", // "blog" is the applied TARGET_NAME for the Hosting site "myapp-blog" "public": "blog/dist", // contents of this folder are deployed to the site "myapp-blog" // ... }, { "target": "app", // "app" is the applied TARGET_NAME for the Hosting site "myapp-app" "public": "app/dist", // contents of this folder are deployed to the site "myapp-app" // ... "rewrites": [...] // You can define specific Hosting configurations for each site } ] } { "storage": [ { "target": "main", // "main" is the applied TARGET_NAME for the group of Cloud Storage buckets "rules": "storage.main.rules" // the file that contains the shared security rules } ] }
If you have multiple configurations for your resources, you can create multiple
deploy targets and specify each one in the firebase.json
file. All associated
resources will be deployed together when you run firebase deploy
.
Manage deploy targets
The settings for deploy targets are stored in the .firebaserc
file in your
project directory. You can manage your project's deploy targets by running any
of the following commands from the root of your project directory.
Command | Description |
---|---|
firebase target
|
Lists the deploy targets for your current project directory |
firebase target:remove \
|
Removes a resource from the target to which it's been assigned |
firebase target:clear \
|
Removes all the resources or Hosting site from the specified target |
The target:remove
and target:clear
commands automatically update the deploy
target settings in the .firebaserc
file in your project directory.
Test locally before deploying
Run any of the following commands from the root of your project directory.
Command | Description |
---|---|
firebase emulators:start
|
Emulates all the configured resources in your project directory |
firebase emulators:start \ |
Emulates only the Hosting content and configuration of the specified Hosting site |
firebase emulators:start \
|
Emulates only the rules file for the specified Cloud Storage target |
firebase emulators:start \
|
Emulates only the rules file for the specified Realtime Database target |
Learn more about configuring and using the Firebase Local Emulator Suite.
Deploy specific Firebase resources
Run any of the following commands from the root of your project directory.
Command | Description |
---|---|
firebase deploy
|
Creates a release of all deployable resources in your project directory |
firebase deploy \
|
Deploys only the Hosting content and configuration of the specified Hosting site to the live channel for the site |
firebase hosting:channel:deploy CHANNEL_ID \ |
Deploys only the Hosting content and configuration of the specified Hosting site to a preview channel for the site |
firebase deploy \
|
Deploys only the rules file for the specified Cloud Storage target |
firebase deploy \
|
Deploys only the rules file for the specified Realtime Database target |