| 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 |
Use these instructions to enable continuous deployment from a GitHub repository. The same general steps apply for other Git providers such as GitLab or Bitbucket. |
| 10 |
|
| 11 |
{{% include "/_common/gitignore-public.md" %}} |
| 12 |
|
| 13 |
## Prerequisites |
| 14 |
|
| 15 |
Please complete the following tasks before continuing: |
| 16 |
|
| 17 |
1. [Create](https://accounts.google.com/) a Google account. |
| 18 |
1. [Log in](https://accounts.google.com/) to your Google account. |
| 19 |
1. [Create](https://github.com/signup) a GitHub account. |
| 20 |
1. [Log in](https://github.com/login) to your GitHub account. |
| 21 |
1. [Create](https://github.com/new) a GitHub repository for your project. |
| 22 |
1. [Create](https://git-scm.com/docs/git-init) a local Git repository for your project with a [remote][] reference to your GitHub repository. |
| 23 |
1. Create a Hugo project within your local Git repository and test it with the `hugo server` command. |
| 24 |
1. Commit the changes to your local Git repository and push to your GitHub repository. |
| 25 |
|
| 26 |
## Procedure |
| 27 |
|
| 28 |
Step 1 |
| 29 |
: Create a Firebase project. |
| 30 |
|
| 31 |
1. Visit the [Firebase console][]. |
| 32 |
|
| 33 |
1. Click **Get started by setting up a Firebase project**. |
| 34 |
|
| 35 |
 |
| 36 |
|
| 37 |
1. Enter your project name and, if your Google account is associated with a Google Workspace or Cloud Identity organization, select a parent resource. Then press the **Continue** button. |
| 38 |
|
| 39 |
 |
| 40 |
|
| 41 |
1. Disable Gemini for this project, then press the **Continue** button. |
| 42 |
|
| 43 |
 |
| 44 |
|
| 45 |
1. Disable Google Analytics for this project, then press the **Create project** button. |
| 46 |
|
| 47 |
 |
| 48 |
|
| 49 |
1. When your Firebase project is ready, press the **Continue** button. |
| 50 |
|
| 51 |
 |
| 52 |
|
| 53 |
1. From the sidebar menu, choose **Settings** > **General** and note the Project ID. You will need the Project ID later in this procedure. |
| 54 |
|
| 55 |
 |
| 56 |
|
| 57 |
1. From the sidebar menu, choose **Settings** > **Service accounts**. At the bottom of the page, press the **Generate new private key** button, then press the **Generate key** button. |
| 58 |
|
| 59 |
 |
| 60 |
|
| 61 |
1. Download the generated JSON file to any location outside of your project directory. |
| 62 |
|
| 63 |
Step 2 |
| 64 |
: Add the private key to GitHub Secrets. |
| 65 |
|
| 66 |
1. Go to your GitHub repository. |
| 67 |
1. Navigate to **Settings** > **Secrets and variables** > **Actions**. |
| 68 |
1. Click the **New repository secret** button. |
| 69 |
1. Enter `FIREBASE_SERVICE_ACCOUNT_KEY` for the Name. |
| 70 |
1. Paste the contents of the downloaded JSON file into the Secret field. |
| 71 |
1. Press the **Add secret** button. |
| 72 |
1. Delete the downloaded JSON file. |
| 73 |
|
| 74 |
Step 3 |
| 75 |
: Create a `hugo.yaml` file in the `.github/workflows` directory, adjusting the tool versions and time zone as needed. Set the `FIREBASE_PROJECT_ID` to the Project ID that you noted earlier in this procedure. |
| 76 |
|
| 77 |
```yaml {file=".github/workflows/hugo.yaml" copy=true} |
| 78 |
name: Build and deploy |
| 79 |
env: |
| 80 |
# Define tool versions |
| 81 |
DART_SASS_VERSION: 1.104.0 |
| 82 |
GO_VERSION: 1.27.0 |
| 83 |
HUGO_VERSION: 0.166.0 |
| 84 |
NODE_VERSION: 24.20.0 |
| 85 |
|
| 86 |
# Set the build time zone |
| 87 |
TZ: Europe/Oslo |
| 88 |
|
| 89 |
# Set the Firebase Project ID. |
| 90 |
FIREBASE_PROJECT_ID: hosting-firebase-17fe0 |
| 91 |
on: |
| 92 |
push: |
| 93 |
branches: |
| 94 |
- main |
| 95 |
workflow_dispatch: |
| 96 |
permissions: |
| 97 |
contents: read |
| 98 |
pull-requests: write |
| 99 |
concurrency: |
| 100 |
group: deployment |
| 101 |
cancel-in-progress: false |
| 102 |
defaults: |
| 103 |
run: |
| 104 |
shell: bash |
| 105 |
jobs: |
| 106 |
build: |
| 107 |
runs-on: ubuntu-latest |
| 108 |
steps: |
| 109 |
- name: Checkout |
| 110 |
uses: actions/checkout@v7 |
| 111 |
with: |
| 112 |
submodules: recursive |
| 113 |
fetch-depth: 0 |
| 114 |
lfs: false |
| 115 |
|
| 116 |
- name: Create a local tools directory |
| 117 |
run: | |
| 118 |
mkdir -p "${HOME}/.local" |
| 119 |
|
| 120 |
- name: Install Go |
| 121 |
if: hashFiles('go.mod') != '' |
| 122 |
uses: actions/setup-go@v7 |
| 123 |
with: |
| 124 |
go-version: ${{ env.GO_VERSION }} |
| 125 |
cache: false |
| 126 |
|
| 127 |
- name: Install Node.js |
| 128 |
if: hashFiles('package-lock.json') != '' |
| 129 |
uses: actions/setup-node@v7 |
| 130 |
with: |
| 131 |
node-version: ${{ env.NODE_VERSION }} |
| 132 |
|
| 133 |
- name: Install Dart Sass |
| 134 |
run: | |
| 135 |
echo "Installing Dart Sass ${DART_SASS_VERSION}..." |
| 136 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/sass/dart-sass/releases/download/${DART_SASS_VERSION}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 137 |
tar -C "${HOME}/.local" -xf "${{ runner.temp }}/dart-sass-${DART_SASS_VERSION}-linux-x64.tar.gz" |
| 138 |
echo "${HOME}/.local/dart-sass" >> "${GITHUB_PATH}" |
| 139 |
|
| 140 |
- name: Install Hugo |
| 141 |
run: | |
| 142 |
echo "Installing Hugo ${HUGO_VERSION}..." |
| 143 |
curl -sfL --output-dir "${{ runner.temp }}" -O "https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 144 |
mkdir "${HOME}/.local/hugo" |
| 145 |
tar -C "${HOME}/.local/hugo" -xf "${{ runner.temp }}/hugo_${HUGO_VERSION}_linux-amd64.tar.gz" |
| 146 |
echo "${HOME}/.local/hugo" >> "${GITHUB_PATH}" |
| 147 |
|
| 148 |
- name: Log tool versions |
| 149 |
run: | |
| 150 |
echo "Logging tool versions..." |
| 151 |
command -v sass &> /dev/null && echo "Dart Sass: $(sass --version)" || echo "Dart Sass: not installed" |
| 152 |
command -v go &> /dev/null && echo "Go: $(go version)" || echo "Go: not installed" |
| 153 |
command -v hugo &> /dev/null && echo "Hugo: $(hugo version)" || echo "Hugo: not installed" |
| 154 |
command -v node &> /dev/null && echo "Node.js: $(node --version)" || echo "Node.js: not installed" |
| 155 |
|
| 156 |
- name: Configure Git |
| 157 |
run: | |
| 158 |
echo "Configuring Git..." |
| 159 |
git config --global core.quotepath false |
| 160 |
|
| 161 |
- name: Fetch full Git history |
| 162 |
run: | |
| 163 |
if [[ $(git rev-parse --is-shallow-repository) == true ]]; then |
| 164 |
echo "Fetching full Git history..." |
| 165 |
git fetch --unshallow |
| 166 |
fi |
| 167 |
|
| 168 |
- name: Initialize Git submodules |
| 169 |
run: | |
| 170 |
if [[ -f .gitmodules ]]; then |
| 171 |
echo "Initializing Git submodules..." |
| 172 |
git submodule update --init --recursive |
| 173 |
fi |
| 174 |
|
| 175 |
- name: Install Node.js dependencies |
| 176 |
run: | |
| 177 |
if [[ -f package-lock.json ]]; then |
| 178 |
echo "Installing Node.js dependencies..." |
| 179 |
npm ci |
| 180 |
fi |
| 181 |
|
| 182 |
- name: Cache restore |
| 183 |
id: cache-restore |
| 184 |
uses: actions/cache/restore@v6 |
| 185 |
with: |
| 186 |
path: ${{ runner.temp }}/.cache/hugo |
| 187 |
key: hugo-${{ github.run_id }} |
| 188 |
restore-keys: hugo- |
| 189 |
|
| 190 |
- name: Build |
| 191 |
run: | |
| 192 |
echo "Building the project..." |
| 193 |
hugo build \ |
| 194 |
--gc \ |
| 195 |
--minify \ |
| 196 |
--cacheDir "${{ runner.temp }}/.cache/hugo" |
| 197 |
|
| 198 |
- name: Cache save |
| 199 |
uses: actions/cache/save@v6 |
| 200 |
with: |
| 201 |
path: ${{ runner.temp }}/.cache/hugo |
| 202 |
key: ${{ steps.cache-restore.outputs.cache-primary-key }} |
| 203 |
|
| 204 |
- name: Upload build artifact |
| 205 |
uses: actions/upload-artifact@v7 |
| 206 |
with: |
| 207 |
name: build-artifact |
| 208 |
path: public |
| 209 |
retention-days: 1 |
| 210 |
deploy: |
| 211 |
needs: build |
| 212 |
runs-on: ubuntu-latest |
| 213 |
steps: |
| 214 |
- name: Download build artifact |
| 215 |
uses: actions/download-artifact@v8 |
| 216 |
with: |
| 217 |
name: build-artifact |
| 218 |
path: public |
| 219 |
|
| 220 |
- name: Create Firebase Hosting config |
| 221 |
run: | |
| 222 |
cat << 'EOF' > firebase.json |
| 223 |
{ |
| 224 |
"hosting": { |
| 225 |
"public": "public" |
| 226 |
} |
| 227 |
} |
| 228 |
EOF |
| 229 |
|
| 230 |
- name: Setup Node.js |
| 231 |
uses: actions/setup-node@v7 |
| 232 |
with: |
| 233 |
node-version: ${{ env.NODE_VERSION }} |
| 234 |
|
| 235 |
- name: Install Firebase CLI |
| 236 |
run: npm install -g firebase-tools --no-fund --no-audit --quiet |
| 237 |
|
| 238 |
- name: Deploy |
| 239 |
env: |
| 240 |
FIREBASE_SERVICE_ACCOUNT_KEY: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_KEY }} |
| 241 |
run: | |
| 242 |
echo "$FIREBASE_SERVICE_ACCOUNT_KEY" > "${RUNNER_TEMP}/gcp_key.json" |
| 243 |
export GOOGLE_APPLICATION_CREDENTIALS="${RUNNER_TEMP}/gcp_key.json" |
| 244 |
firebase deploy --only hosting --project "${FIREBASE_PROJECT_ID}" |
| 245 |
``` |
| 246 |
|
| 247 |
Step 4 |
| 248 |
: In the project configuration file in the root of your local Git repository, set the [`baseURL`][] to the assigned URL as shown below. The assigned URL is composed of the Project ID that you noted earlier in this procedure, followed by the `web.app` domain name. |
| 249 |
|
| 250 |
{{< code-toggle file=hugo >}} |
| 251 |
baseURL = 'https://hosting-firebase-17fe0.web.app/' |
| 252 |
locale = 'en-US' |
| 253 |
title = 'Hosting Test - Firebase' |
| 254 |
{{< /code-toggle >}} |
| 255 |
|
| 256 |
Step 5 |
| 257 |
: In the project configuration file in the root of your local Git repository, set the location of the image cache to the [`cacheDir`][] as shown below. |
| 258 |
|
| 259 |
{{< code-toggle file=hugo copy=true >}} |
| 260 |
[caches.images] |
| 261 |
dir = ':cacheDir/images' |
| 262 |
{{< /code-toggle >}} |
| 263 |
|
| 264 |
See [configure file caches][] for more information. |
| 265 |
|
| 266 |
Step 6 |
| 267 |
: Commit the changes to your local Git repository and push to your GitHub repository. |
| 268 |
|
| 269 |
Step 7 |
| 270 |
: From GitHub's main menu, choose **Actions**. You will see something like this: |
| 271 |
|
| 272 |
 |
| 273 |
|
| 274 |
Step 8 |
| 275 |
: When GitHub has finished building and deploying your site, the color of the status indicator will change to green. |
| 276 |
|
| 277 |
 |
| 278 |
|
| 279 |
In the future, whenever you push a change from your local Git repository, GitHub will rebuild and deploy your site. |
| 280 |
|
| 281 |
## Related resources |
| 282 |
|
| 283 |
For more information on hosting and managing your site with Firebase Hosting, consult the official documentation: |
| 284 |
|
| 285 |
- [General documentation][] |
| 286 |
- [Custom domain setup][] |
| 287 |
|
| 288 |
[Custom domain setup]: https://firebase.google.com/docs/hosting/custom-domain |
| 289 |
[Firebase console]: https://console.firebase.google.com/ |
| 290 |
[General documentation]: https://firebase.google.com/docs/hosting |
| 291 |
[`baseURL`]: /configuration/all/#baseurl |
| 292 |
[`cacheDir`]: /configuration/all/#cachedir |
| 293 |
[configure file caches]: /configuration/caches/ |
| 294 |
[remote]: https://git-scm.com/docs/git-remote |