January 19, 2019

Using Github Actions To Deploy To Firebase

I wrote an article ages ago about how I set up a blog using CircleCI and Firebase. This was a really nice workflow where I could get near instant updates with just a git push. Given I’m a cheapskate once github annonuced they are giving everyone free private repos I started to move all my stuff away from bitbucket back to github since I really like their dev experience and I’m in there all the time.

As I moved over the blog repo I saw that I had access to github actions and thought “Hmmm, I wonder if I can use this to deploy to firebase”, the answer is yes and it’s wonderful. Here I’m just going to show you a dead simple way that I got everything moved from CircleCI onto github actions to lower the amount of tools I’m using. I really do love CircleCI and think it’s a great tool but I only need to run a few commands and if I can keep that all in github (I know this is what Microsoft wants, they want devs to never leave their product) and make my life simpler then yeah I’m all about that. It’s not that I don’t love your tool, it’s just that I don’t love complexity.

It's not you, it's me

In your github repo you should have an Actions tab in your repo nav bar. Click that.

Blog Repository

From there you’ll be taken to the workflow screen and you can choose some prebaked actions to choose from. However none here are what we need so we will need a custom one. The sweet thing about actions is that it’s just a docker image. If you can build a docker image and run some commands then your limit is your imagination. For custom actions you can either use a public one that someone has written or you can make your own. We’ll make our own.

As I said before an action is just a docker image so the docker image we used in my last blog post to deploy can be repurposed here. Yay for portable containers! And now we just need to build a docker image on top of that with an entrypoint script that did the same steps our CircleCI config did before. Our action docker file will live in actions/firebase/Dockerfile and look like:

FROM jhinds/firebase-deploy-action:0.0.1

LABEL "com.github.actions.name"="Firebase"
LABEL "com.github.actions.description"="Run firebase commands"
LABEL "com.github.actions.icon"="zap"
LABEL "com.github.actions.color"="orange"

LABEL "repository"="http://github.com/jhinds/blog"
LABEL "maintainer"="Jonathan Hinds <[email protected]>"

COPY entrypoint.sh /entrypoint.sh
CMD ["/entrypoint.sh"]

and the actions/firebase/entrypoint.sh script will look like:

#!/bin/sh -l

set -eu

git submodule sync
git submodule update --init
hugo
firebase deploy --token $FIREBASE_TOKEN

nice and lastly we need a workflow file in .github/main.workflow which will look like:

workflow "Deploy Workflow" {
  on = "push"
  resolves = ["Firebase"]
}

action "Firebase" {
  uses = "./actions/firebase"
  secrets = ["FIREBASE_TOKEN"]
}

Cool. Let’s commit that and push it. The workflow will build and fail because for the last step we need to tell github what our FIREBASE_TOKEN is.

To do that we go to the UI and add it to our workflow and click on our Firebase action and then on Edit.

Firebase Action

From there on the action config go to secrets and add in the key and value of the secret and you can use them as environment variables, that’s it.

Firebase Action Edit

Now if you were to re run the workflow it should work. A nice and simple swap out from CircleCI.

GitHub actions are more powerful than my simple firebase deploy, you can do some pretty complex stuff. But at the same time it’s pretty great for small tasks like this so it scales from use case to use case. Hopefully now with actions you’ll automate more of your workflows and reduce the complexity of your toolset.