This page explains how to deploy applications to Firebase using Cloud Build. If you're new to Cloud Build, read the quickstarts and the Build configuration overview first.
Before you begin
-
Enable the Cloud Build, Firebase, and Resource Manager APIs.
To run the
gcloud
commands on this page, install the Google Cloud CLI.Keep your application source code including
firebase.json
handy. Your source code needs to be stored in a repository, such as Cloud Source Repositories, GitHub, or Bitbucket.If you don't already have a project to deploy to Firebase, you can create a default project by installing and initializing Firebase.
Required IAM permissions
Open the IAM page in the Google Cloud console:
Select your project and click Open.
In the permissions table, locate the email for the service account you are using for the build and click the pencil icon.
Add
Cloud Build Service Account
,Firebase Admin
andAPI Keys Admin
roles.Click Save.
Using the firebase
community builder
Cloud Build provides a
Firebase community builder image that you can use to invoke firebase
commands
in Cloud Build. To use this builder in a Cloud Build config
file, you must first build the image and push it to the Container Registry in
your project.
To build and push the firebase
community builder:
Navigate to your project root directory.
Clone the cloud-builders-community repository:
git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git
Navigate to the
firebase
builder image:cd cloud-builders-community/firebase
Submit the builder to your project, where
REGION
is one of the supported build regions:gcloud builds submit --region=REGION .
Navigate back to your project root directory:
cd ../..
Remove the repository from your root directory:
rm -rf cloud-builders-community/
Configuring the deployment
After building the firebase
community builder, you can use the firebase
build
step in your build config file to deploy to Firebase:
Create a build config file named
cloudbuild.yaml
orcloudbuild.json
whereproject-id
is your Google Cloud project ID andfirebase-project-id
is your Firebase project ID:YAML
steps: - name: gcr.io/project-id/firebase args: ['deploy', '--project=firebase-project-id', '--only=hosting']
JSON
{ "steps": [ { "name": "gcr.io/project-id/firebase", "args": [ "deploy", "--project", "firebase-project-id", "--only", "hosting" ] } ] }
Start the build using the build config file:
gcloud builds submit --region=REGION --config config-file-path source-directory
Where:
- config-file-path is the path to the build config file.
- source-directory is the path or URL to the source code.
REGION
is one of the supported build regions.
Continuous deployment
You can automate the deployment of your software to Firebase by creating Cloud Build triggers. You can configure triggers to build and deploy images whenever you update your source code.
To automate your deployment to Firebase:
In your repository, add a build config file with steps to invoke the
firebase deploy
command where project-id is your Google Cloud project ID:YAML
steps: - name: gcr.io/project-id/firebase args: ['deploy', '--project=project-id', '--only=hosting']
JSON
{ "steps": [ { "name": "gcr.io/project-id/firebase", "args": [ "deploy", "--project", "project-id", "--only", "hosting" ] } ] }
Create a trigger with the build config file created in the previous step:
Open the Triggers page in the Google Cloud console:
Select your project from the project selector drop-down menu at the top of the page.
Click Open.
Click Create trigger.
On the Create trigger page, enter the following settings:
Enter a name for your trigger.
Select the repository event to start your trigger.
Select the repository that contains your source code and build config file.
Specify the regex for the branch or tag name that will start your trigger.
Configuration: Choose the build config file you created previously.
Click Create to save your build trigger.
Anytime you push new code to your repository, you will automatically start a build and deploy on Firebase.
For more information on creating Cloud Build triggers, see Creating and managing build triggers.
Code example
To view a code sample for deploying to Firebase using Cloud Build, go to deploy-firebase-example.
What's next
- Learn how to perform blue/green deployments on Compute Engine
- Learn how to deploy on Cloud Run
- Learn how to deploy on GKE
- Learn how to deploy on Cloud Run functions
- Learn how to deploy on App Engine
- Learn how to troubleshoot build errors