The following Github Action will successfully build and deploy your Hugo static-site and upload it to Google Firebase Hosting.
Pre-requisites
- You will need a Firebase Project. If you haven't already got one you can easily get one here: Firebase Console
- You will need a Google Service Account setup in-order for your site to be successfully deployed. See below on how to set one up.
- Visit the GCP Service Accounts page and make sure the correct project (same name as your Firebase project) is selected in the top blue bar
- Click the "+ CREATE SERVICE ACCOUNT" button
- Give the service account a name, id, description. I recommend something like
github-action-<my repository name> - At the "Grant this service account access to project" step, choose the following roles that the service account will need to deploy on your behalf:
- Firebase Authentication Admin (Required to add preview URLs to Auth authorized domains)
roles/firebaseauth.admin
- Firebase Hosting Admin (Required to deploy preview channels)
roles/firebasehosting.admin
- Cloud Run Viewer (Required for projects that use Hosting rewrites to Cloud Run or Cloud Functions)
roles/run.viewer
- API Keys Viewer (Required for CLI deploys)
roles/serviceusage.apiKeysViewer
- Firebase Authentication Admin (Required to add preview URLs to Auth authorized domains)
- Finish the service account creation flow
- Create and download the new service account's JSON key
- Add that JSON key as a secret in your GitHub repository. I recommend a name like
FIREBASE_SERVICE_ACCOUNTbut ensure it has been base64 encoded before saving. You can do this on MacOS by runningcat {file_name}.json | base64in Terminal and copying the output into Github.
- Ensure you've followed the above steps to setup your service account.
- Log in to Firebase (setup on your local machine) using
firebase login, which opens a browser where you can select your account. Usefirebase logoutin case you are already logged in but to the wrong account. - In the root of your Hugo project, initialize the Firebase project with the
firebase initcommand. From here:- Choose Hosting in the feature question
- Choose the project you just set up
- Accept the default for your database rules file
- Accept the default for the publish directory, which is public
- Choose “No” in the question if you are deploying a single-page app
- You will now have everything setup within your repo to deploy successfully to Firebase.
- Once completed you are ready to setup your action.
Within your repo select the Actions tab. Create a new workflow and paste the following code:
on: [push]
name: Deploy Hugo to Firebase
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
submodules: true
- uses: ryank90/action-hugo-firebase-deploy@master
with:
firebaseServiceAccount: ${{ secrets.firebase_service_account }}
projectId: ${{ secrets.firebase_project_name }}
hugoParams: -D
Save and you are ready to push code to your repo and have it automatically deployed.
firebaseServiceAccount {string} required
This is a Google service account JSON key that has been base64 encoded. Once you have got your JSON key configured and downloaded you can run `` on your mac to base64 encode it ready for uploading into Github.
It's important to ensure you set this up as an encrypted secret within Github. You can do that here: https://github.com/USERNAME/REPOSITORY/settings/secrets.
projectId {string}
This is your Firebase project ID that can be found from within the Firebase console.
hugoParams {string}
These are your hugo parameters that you would like to pass. It defaults to -D which will built your site ready for deployment although you can pass further parameters. See Hugo documentation for more information on what is available here.