In Prod

← Back to Posts

Firebase Hosting and GitHub Actions

February 10, 2020

Firebase Hosting is a free static site hosting platform that we can deploy to using CLI tooling. GitHub Actions are workflows for repos that can provide continuous integration and continuous deployment (CI/CD).

Firebase

Make sure you have a project in the Firebase Console and enable hosting in the project settings.

You can use your own custom domain, and it comes with free SSL.

Install the Firebase CLI globally:


_1
yarn global add firebase-tools

Initialise firebase:


_1
firebase init

Choose your project and select Hosting.

The public folder should be the folder you have set up to output to in your build process. By default, in a create-react-app project, this is the build folder.

If you have a single page app choose yes to rewrite all routes to index.html. Once complete you should get a json file the resembles this:

firebase.json

_16
{
_16
"hosting": {
_16
"public": "build",
_16
"ignore": [
_16
"firebase.json",
_16
"**/.*",
_16
"**/node_modules/**"
_16
],
_16
"rewrites": [
_16
{
_16
"source": "**",
_16
"destination": "/index.html"
_16
}
_16
]
_16
}
_16
}

Auth

For our process to work in GitHub Actions we need to be able to log in to firebase with a machine user. The Firebase CLI has a convenience command for this that generates an access token that can be used to authenticate from the CLI:


_1
firebase login:ci

This will open a login prompt in browser for you to consent to, and once complete the token will be written back in the shell. This will need to be added as a secret in your GitHub repo.

GitHub Actions

In your projects repo on GitHub, add the auth token from the previous step as a secret (give it a name like FIREBASE_TOKEN).

Once that is done we can create a new workflow in the Actions tab. The below file simply triggers on any push to master, then installs, tests, builds, and deploys:

.github/workflows/main.yml

_31
name: CI/CD
_31
_31
on:
_31
push:
_31
branches:
_31
- master
_31
_31
jobs:
_31
build:
_31
name: Build
_31
runs-on: ubuntu-latest
_31
steps:
_31
- uses: actions/checkout@master
_31
_31
- name: Install Dependencies
_31
run: yarn install
_31
_31
- name: Run tests
_31
run: yarn test
_31
_31
- name: Build Production Artifact
_31
run: yarn build
_31
_31
# make sure we have the latest firebase cli globally available to run our commands
_31
# needs sudo to be able to access the commands
_31
- name: Install Firebase CLI
_31
run: sudo npm install -g firebase-tools
_31
_31
# use the secret created in the repo to authenticate with Firebase
_31
- name: Deploy to Firebase
_31
run: sudo firebase deploy --token "${{ secrets.FIREBASE_TOKEN }}"

Once complete the site should be live on the setup domain.


Andrew McMahon
These are a few of my insignificant productions
by Andrew McMahon.