Firebase on Github Actions
Tags: firebase, github, docker, devops
Published: 2018-12-12 19:09:59

---
title: Firebase on Github Actions
published: true
description: Using Github Actions to automate projects that utilize the power of Firebase
tags: #firebase #github #docker #devops
---

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:

```bash
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 w9jds/firebase-action %}