Firebase Cloud Functions Continuous Deploying with Cloud Build

Considerations:

  • I made this approach to work with several projects and services.
  • You will need to change to the Blaze plan and activate billing (this is important if you are using firebase in production and you want to keep track of your cost).
  • I will assume you have several projects, for the sake of the example, I will use only two production and staging.

The Setup

  • my-company-dev
  • my-company-prod
CI build
  • All secrets are going to be handled via the production account.
  • Commits to the master and staging branch in github will trigger the process. How to should manage your PRs is out of the scope of this post.

The process

project tree
.firebaserc

Installing the gcloud SDK

gcloud init

Configuring Cloud Build

  • Check Cloud Build configuration file
  • Add one Item (or more if you have environment variables) and set a substitution _PROJECT_NAME, important to start with _.

Upload the Firebase Builder

git clone https://github.com/GoogleCloudPlatform/cloud-builders-community
cd cloud-builders-community/firebase
gcloud builds submit --config cloudbuild.yaml .
gcloud config set project my-company-prod

Secrets

firebase use production // our whatever you main project is
firebase login:ci
  • Encrypt it using the Cryptographic Keys services.
  • Encode 64 it.
  • Add it to the clodbuild.yml file.

Cryptographic Keys

  • Create a ring name ci-ring
  • Then create a key called deployment

Encrypt Firebase token

gcloud kms encrypt --plaintext-file=.env --ciphertext-file=.env.enc --location=global --keyring=ci-ring --key=deployment

Encode base64

openssl base64 -in .env.enc -out .env.enc.txt

Create the cloud build configuration file

projects/my-company-prod/locations/global/keyRings/ci-ring/cryptoKeys/deployment
- name: 'gcr.io/cloud-builders/npm'
dir: 'functions'
args: ['run', 'test']
- name: 'gcr.io/$PROJECT_ID/firebase'
args: ['deploy', '--only', 'firestore:rules']

IAM Roles

Test

gcloud builds submit . --config=cloudbuild.yaml --substitutions=_PROJECT_NAME=my-company-dev

That’s all

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andrés Mijares

Andrés Mijares

yep.. i’m an asshole🤷🏻‍♂️