DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Jeremy Shore
Jeremy Shore

Posted on

Firebase on Github Actions

You have probably heard that Github is releasing a workflow system of their own that is obscenely powerful. They even include a huge amount of out of the box actions that will get a lot of things done for you like deploying to npm, Azure, and Google Cloud Platform. However, they don’t offer anything for Firebase which has a whole sweet of offerings like hosting, functions, real-time database, and Firestore.

Since actions can basically be anything with a Dockerfile, I have created a repo that sets up the environment so you can use any firebase-tools commands you need in your workflow. I have been using it on my private repositories to deploy to dev and prod hosting sites as I push them to specific branches.

On your development machine, you need to get a CI token from Firebase, so in your terminal just enter:

firebase login:ci

This will NOT mess up your current login on your machine and generates a completely new token to use.

Go through the login process and copy the token that is spit out into your console, and navigate to the project you want to include a Firebase action in. Inside of your workflow create a new action and for uses you want to set it to w9jds/firebase-action@master (It is recommended to use a specific commit hash over master as it won’t break as actions are updated!).

In this action there is a secrets section. Click create new secret and create a token named FIREBASE_TOKEN using the token you just recieved. Once you finish, make sure that the secret is checked for this action.

Depending on what you want to do in this action, update the args for the action to what you want. Actions are recommended to default to any help commands, so you want to override it. Once set up, it should look something like this:

action "Pushed to develop" {
  uses = "actions/bin/filter@95c1a3b"
  args = "branch develop"
}
action "Install Dependencies" {
  uses = "actions/npm@6309cd9"
  needs = ["Pushed to develop"]
  args = "install"
}
action "Build Development" {
  uses = "actions/npm@6309cd9"
  needs = ["Install Dependencies"]
  secrets = ["FIREBASE_API_KEY"]
  args = "run build"
}
action "Deploy to Development" {
  uses = "w9jds/firebase-action@master"
  needs = ["Build Development"]
  args = "deploy --only hosting:dev"
  env = {
    PROJECT_ID = "new-eden-storage-a5c23"
  }
  secrets = ["FIREBASE_TOKEN"]
}

Notice that I have a workflow that first installs all of the dependencies needed for my project. It then moves on to run a build for the project, and finally deploys the project to the dev hosting target on my firebase project.

Once this workflow runs you should see the firebase action output something like this:

=== Deploying to 'new-eden-storage-a5c23'...

i  deploying hosting
i  hosting[galaxyfinder-dev]: beginning deploy...
i  hosting[galaxyfinder-dev]: found 16 files in build
i  hosting: adding files to version [0/16] (0%)
i  hosting: uploading new files [5/6] (83%)
βœ”  hosting[galaxyfinder-dev]: file upload complete
i  hosting[galaxyfinder-dev]: finalizing version...
βœ”  hosting[galaxyfinder-dev]: version finalized
i  hosting[galaxyfinder-dev]: releasing new version...
βœ”  hosting[galaxyfinder-dev]: release complete

βœ”  Deploy complete!

As this action isn’t designed to only work for deployment, if the command is supported by firebase-tools then you can use it as an argument.

Here is the firebase action if you would like to see the source and documentation:

GitHub logo w9jds / firebase-action

GitHub Action for interacting with Firebase

GitHub Actions for Firebase

This Action for firebase-tools enables arbitrary actions with the firebase command-line client.

Inputs

  • args - Required. This is the arguments you want to use for the firebase cli

Environment variables

  • FIREBASE_TOKEN - Required if GCP_SA_KEY is not set. The token to use for authentication. This token can be aquired through the firebase login:ci command.

  • GCP_SA_KEY - Required if FIREBASE_TOKEN is not set. A base64 encoded private key (json format) for a Service Account with the Firebase Admin role in the project, and if your deploying functions you would also need the Cloud Functions Developer role And since the deploy service account is using the App Engine default service account in the deploy process, it also needs the Service Account User role If your only doing Hosting Firebase Hosting Admin is enough.

  • PROJECT_ID - Optional. To specify a specific project to use for…

Top comments (0)

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.