| 1 |
--- |
| 2 |
title: Host on Firebase |
| 3 |
description: Host your project on Firebase. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
aliases: [/hosting-and-deployment/hosting-on-firebase/] |
| 7 |
--- |
| 8 |
|
| 9 |
## Assumptions |
| 10 |
|
| 11 |
1. You have an account with [Firebase][signup]. |
| 12 |
1. You have completed the [Quick Start][] or have a completed Hugo website ready for deployment. |
| 13 |
|
| 14 |
## Initial setup |
| 15 |
|
| 16 |
Go to the [Firebase console][console] and create a new project (unless you already have a project). You will need to globally install `firebase-tools` (node.js): |
| 17 |
|
| 18 |
```sh |
| 19 |
npm install -g firebase-tools |
| 20 |
``` |
| 21 |
|
| 22 |
Log in to Firebase (setup on your local machine) using `firebase login`, which opens a browser where you can select your account. Use `firebase logout` in case you are already logged in but to the wrong account. |
| 23 |
|
| 24 |
```sh |
| 25 |
firebase login |
| 26 |
``` |
| 27 |
|
| 28 |
In the root of your Hugo project, initialize the Firebase project with the `firebase init` command: |
| 29 |
|
| 30 |
```sh |
| 31 |
firebase init |
| 32 |
``` |
| 33 |
|
| 34 |
From here: |
| 35 |
|
| 36 |
1. Choose Hosting in the feature question |
| 37 |
1. Choose the project you just set up |
| 38 |
1. Accept the default for your database rules file |
| 39 |
1. Accept the default for the publish directory, which is `public` |
| 40 |
1. Choose "No" in the question if you are deploying a single-page app |
| 41 |
|
| 42 |
## Using Firebase & GitHub CI/CD |
| 43 |
|
| 44 |
In new versions of Firebase, some other questions apply: |
| 45 |
|
| 46 |
1. Set up automatic builds and deploys with GitHub? |
| 47 |
|
| 48 |
Here you will be redirected to login in your GitHub account to get permissions. Confirm. |
| 49 |
|
| 50 |
1. For which GitHub repository would you like to set up a GitHub workflow? (format: user/repository) |
| 51 |
|
| 52 |
Include the repository you will use in the format above (Account/Repo) |
| 53 |
Firebase script with retrieve credentials, create a service account you can later manage in your GitHub settings. |
| 54 |
|
| 55 |
1. Set up the workflow to run a build script before every deploy? |
| 56 |
|
| 57 |
Here is your opportunity to include some commands before you run the deploy. |
| 58 |
|
| 59 |
1. Set up automatic deployment to your site's live channel when a PR is merged? |
| 60 |
|
| 61 |
You can let in the default option (main) |
| 62 |
|
| 63 |
After that Firebase has been set in your project with [CI/CD](g). After that run: |
| 64 |
|
| 65 |
```sh |
| 66 |
hugo build && firebase deploy |
| 67 |
``` |
| 68 |
|
| 69 |
With this you will have the app initialized manually. After that you can manage and fix your GitHub workflow from <https://github.com/your-account/your-repo/actions>. |
| 70 |
|
| 71 |
Don't forget to update your static pages before push! |
| 72 |
|
| 73 |
## Manual deploy |
| 74 |
|
| 75 |
To deploy your Hugo site, execute the `firebase deploy` command, and your site will be up in no time: |
| 76 |
|
| 77 |
```sh |
| 78 |
hugo && firebase deploy |
| 79 |
``` |
| 80 |
|
| 81 |
## CI setup (other tools) |
| 82 |
|
| 83 |
You can generate a deploy token using |
| 84 |
|
| 85 |
```sh |
| 86 |
firebase login:ci |
| 87 |
``` |
| 88 |
|
| 89 |
You can also set up your CI and add the token to a private variable like `$FIREBASE_DEPLOY_TOKEN`. |
| 90 |
|
| 91 |
> [!NOTE] |
| 92 |
> This is a private secret and it should not appear in a public repository. Make sure you understand your chosen CI and that it's not visible to others. |
| 93 |
|
| 94 |
You can then add a step in your build to do the deployment using the token: |
| 95 |
|
| 96 |
```sh |
| 97 |
firebase deploy --token $FIREBASE_DEPLOY_TOKEN |
| 98 |
``` |
| 99 |
|
| 100 |
## Reference links |
| 101 |
|
| 102 |
- [Firebase CLI Reference][] |
| 103 |
|
| 104 |
[Firebase CLI Reference]: https://firebase.google.com/docs/cli/#administrative_commands |
| 105 |
[Quick Start]: /getting-started/quick-start/ |
| 106 |
[console]: https://console.firebase.google.com/ |
| 107 |
[signup]: https://console.firebase.google.com/ |