Firebase Continuous Integration and Deployment (CI/CD) with Google Cloud Build

Rohan Singh
FAUN — Developer Community 🐾
5 min readDec 28, 2020

--

In this December month article, I’ll be walking you through the CI/CD of Firebase with Google Cloud Build.

Firebase CI/CD with Google Cloud Build

Firebase

Firebase is an amazing platform totally dedicated to mobile and web apps from startups to global enterprises, it’s backed by Google and comes with pre-packaged, open-source bundles of code to automate common development tasks and integrating firebase with other tool is super-easy.

Checkout Vrijraj Singh medium article, he is Google Developer Expert for Firebase & has written informatively on Firebase and offerings.

Firebase Docs

Google Cloud Build

Cloud Build is a serverless CI/CD service offered by GCP. With Cloud Build, you can deploy software quickly across all programming languages across multiple environments, and it performs deep security scans of your CI/CD pipeline.

I really love working on Google Cloud Build as it super easy to set up, flexibly integrates with GitHub and Bitbucket, serverless and secure, cheap pricing, and super-fast deployment.

Google Cloud Build Docs

Workflow Diagram

Enable APIs

From your GCP Console, enable:

  • Google Cloud Build
  • Secret Manager

Get Firebase CI Token

For deployment of your web app, we need a unique firebase ci token. This token we’ll be storing in Secret Manager and supply it on the runtime of Cloud Build trigger. In this way, your unique token remains safe and only accessible to Cloud Build at the time of deployment.

Run to get token:

firebase login:ci

Create Secret in GCP Secret Manager

Create one secret from Google Cloud Console➞Security➞Secret Manager with firebase unique token. Enter token in Secret value text area.

One of the best things about Secret Manager, it provides versioning of secret, so if your secret gets change, from secret manager secret, you can add a new version of the secret. You can disable and destroy previous and unused versions.

Secret Manager Secret Versions

Dockerfile

We will be using dockerfile for the deployment of our web app over Firebase. With dockerfile, I can easily deploy my web app over firebase and can keep a docker image in Google Container Registry.

In the above dockerfile the base image is node, I’ve only installed firebase tools, gcloud and set path to local. We need to build docker image only once and push it to GCR, after that we will using this docker image to start a container and run the necessary commands for us.

Cloud Build Config File

A build config file defines the fields that are needed for Cloud Build to perform your tasks. We create .yml or .json file for cloud build where we write instructions for our CI/CD.

Read more about Cloud Build Config File

Config file starts with steps field. For each step in steps, cloud build spin up a docker container and it will spin down the docker container once the task completed.

In name we specify cloud-builders or docker container images, here we spinning up docker cloud-builder.

id is used to give a unique identifier to the step.

The args field of a build step takes a list of arguments and passes them to the builder referenced by the name field.

The dir field in a build step is used to set a working directory when running the step. If your config file has more than one build step, the assets produced by one step can be passed to the next one via the persistence of the /workspace directory, which allows you to set up a pipeline of build steps that share assets.

entrypoint in a build step is given to specify an entry point if you don't want to use the default entry point of the builder. Most builders have bash installed. If you don't set this field, Cloud Build will use the builder's entrypoint.

Read more about Cloud Build Syntax

The above config file has only 1 step — I’m deploying an application to firebase with bash entrypoint. I’m fetching Firebase Token from Secret Manager that we generated and stored in Secret Manager before and supplying it to firebase deploy command along with --project-id and --token flag via FIREBASE_TOKEN variable. There are two $$ (escape characters) before FIREBASE_TOKEN which specifies it’s no substitution value, rather cloud build will pass its value to the container.

NOTE: Put this firebase-cloudbuild.yml in the root folder of repo.

Create Cloud Build Trigger

From Cloud Build➞Triggers, connect GitHub repository, it will install Cloud Build GitHub App in your repository. Once the GitHub repo is connected now let’s create a trigger that would trigger whenever there is a new commit in your repo.

Give a unique name to your trigger. For Event — Select Push to Branch.

In Source put branch name in regex (example: ^master$) and put public/* in Included files filter (glob). This section will ensure only to trigger pipeline when there is any change in public folder of master branch in GitHub. For pipelines dedicated to feature-branch , we need to check the invert regex option.

Google Cloud Build Trigger

In Build configuration, put the cloud build config file-name. From AdvancedSubstitution Variables, we need to provide the value of variables defined in the cloud build config file.

Google Cloud Build Variables Substitution

Now, when there is any change in the public folder of master branch, cloud build will get triggered, our container will spin up, run firebase deploy command and then spin down.

Google Cloud Build Trigger Log

Bingo!!!

This is how you integrate Google Cloud Build with Firebase for CI/CD.

Firebase Docs

Google Cloud Build

Google Cloud Secret Manager

Cloud Build Config File

Read my previous blogs

Clap and share it if you found it useful!!!

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--