Use Firestore to Build Hugo Conten
Tags: podcast, webdev, javascript, beginners
Published: 2022-08-30 19:28:06


This tutorial will explore some fairly complex triggers and Git practices.

Demo: <br/> [Angular Admin Site](https://ajonp-lesson-8-admin.firebaseapp.com/)<br/> [Hugo Main Site](https://ajonp-lesson-8-hugo.firebaseapp.com/)

## Steps

- Billing Limit Reminder

- Fork/Clone lesson-8-hugo

- Create Hugo firebase hosting site

- Create Hugo Content Google Cloud Build Trigger

- Test Cloud Build Trigger

- Fork/Clone lesson-8-firestore-functions

- Build Angular Project

- Deploy Firebase Hosting from CLI

- Update Firestore Trigger to match your GitHub repo

- Deploy Firebase Functions from CLI

- Add a book, watch it show in Hugo site

Optional

- Cloud Build for CI/CD

# Billing Limit Reminder

> Reminder to try and stay as free as possible. You should not run into problems, but just in case. In your [Billing](https://console.cloud.google.com/billing/) Dashboard create a new Budget for your project incase you try this more than say 50 times.

If you want to put a hard cap on builds checkout [Capping API Usage](https://cloud.google.com/apis/docs/capping-api-usage).

For the build API specifically you can go to [Cloud Build API Quotas](https://console.cloud.google.com/apis/api/cloudbuild.googleapis.com/quotas)

To update checkout [IAM & Admin->Quotas](https://console.cloud.google.com/iam-admin/quotas)

# Fork/Clone lesson-8-hugo

You can just clone [lesson-8-hugo](https://github.com/AJONPLLC/lesson-8-hugo) from GitHub but if you want to build out any triggers for Google Cloud building, I would suggest forking to your own repo.

## Clone your Fork

I will run through our example by forking to my ajonp account, you should see it "forked from AJONPLLC/lesson-8-hugo"

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/3993ee6b-f3e9-4c05-bfa4-42de3f5f937e.png)

You can do this by replacing your_name in this command.

```bash
git clone https://github.com/<your_name>/lesson-8-hugo.git && cd lesson-8-hugo
```

## Test serving hugo locally

Your theme folder will be empty as it references a git submodule. First make sure that you have all of your submodules cloned locally.

```bash
git submodule init && git submodule update --remote
```

Now lets run the hugo serve command. This tells hugo to serve using ajonp-hugo-ionic theme and use the config.toml file.

```bash
hugo server -t ajonp-hugo-ionic --config config.toml
```

You should now see the home page listing the latest books at `http://localhost:1313/`.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/f1605505-09b9-4ca1-b89c-568b66730ba9.png)

You can also view all the books in a list format at http://localhost:1313/books/.

![](https://media.codingcat.dev/image/upload/v1657636768/main-codingcatdev-photo/ae6185a5-4aaf-4ba9-9ca8-cf1c0f7930b7.png)

This will be based on whatever the latest content had in `AJONPLLC/lesson-8-hugo` if you don't want any of it feel free to clear all files in `content/books`.

> Remember Hugo dynamically builds files, so if you delete all of the files in `content/books` you will get a 404 on `http://localhost:1313/books/`.

# Create Hugo firebase hosting site

## Create firebase project

Start by Adding a new Project

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/1c457c4b-4038-469e-bd96-6dd41acabd87.png)

Give the project a good name.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/1c59765e-b19d-491d-82e1-b1b625e1c267.jpg)

You can then follow the Getting started under hosting, or follow the update local firebase files.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/03499770-bed4-4839-a589-e3a7dc0cf5b6.png)

## Update local firebase files

There are a couple firebase commands that you could use to manually add the correct hosting setup `firebase use ajonp-lesson-8` and `firebase target:apply hosting ajonp-lesson-8-hugo ajonp-lesson-8-hugo`. However, I feel it is easier to just update two files.

`.firebaserc`

```json
{
  "projects": {
    "default": "ajonp-lesson-8"
  },
  "targets": {
    "ajonp-lesson-8": {
      "hosting": {
        "ajonp-lesson-8-hugo": [
          "ajonp-lesson-8-hugo"
        ]
      }
    }
  }
}

```

Your update .firebaserc

```json
{
  "projects": {
    "default": "your-project"
  },
  "targets": {
    "your-project": {
      "hosting": {
        "your-hosting-name": [
          "your-hosting-name"
        ]
      }
    }
  }
}

```

In the above example replace the default project to match your project. For targets this is mapping your project to your hosting names, we will define the hosting name in firebase.json as the target.

firebase.json

```json
{
  "hosting": {
    "target": "ajonp-lesson-8-hugo",
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}
```

Your update firebase.json

```json
{
  "hosting": {
    "target": "your-hosting-name",
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}
```

You will now be able to deploy your site using the firebase CLI.

```bash
firebase deploy
```

You should see a result with around 53 files.

> If you only see a few check that you ran the submodule update.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/3c72283b-db9b-410f-9430-4a6572ce5788.jpg)

# Create Hugo Content Google Cloud Build Trigger

Now that we have manually tested that everything works, we want to validate that we can trigger a Hugo build everytime a commit occurs.

## Google Cloud Project

Every Firebase project is really just a Google Platform Project. In order to use the cloud builder we need to enable billing, I suggest switching to the Blaze plan as you won't need to pay anything if you stay under the limits.

![](https://media.codingcat.dev/image/upload/v1657636768/main-codingcatdev-photo/6e9706b7-76c0-4369-a178-307ccbbc7a2a.png)

Now start by going to [Google Cloud Console](https://console.cloud.google.com/).

You should see a dropdown selection for the project, select the correct project.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/35b7d997-1ab4-4bde-90b3-dd7cb4ff62e8.png)

### Google Cloud Trigger Enable

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/635ebb54-19a4-4df4-a437-35dad920923a.png)

If this is a new project you will need to enable the cloud build API.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/4ab89b5d-e3f6-4ad1-94cd-5e6d5641561d.png)

> If you receive an error, please make sure that you have changed to a Blaze Plan in Firebase.

	![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/9e748293-ca82-476e-b4c5-a11a321cf72e.png)

### Google Cloud Trigger Create

If you need more help on this lesson check our [Google Cloud Repositories CI/CD](https://ajonp.com/lessons/google-cloud-repositories-ci-cd) for a full walk through.

Select the source from GitHub, and consent.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/1ab0b4d8-4a0a-4b74-a03b-8d6133d75ed8.png)

After you authenticate to GitHub choose your project and continue.

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/4afbe623-dfc5-4134-88f2-a59ad20dab04.png)

Settings

- Name: Hugo CI/CD

- Branch: master

- Build configuration: cloudbuild.yaml

- Substitution variables: _FIREBASE_TOKEN = yourtoken.

	![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/124829ab-c442-4e4e-bdf4-a958e6f176f8.png)

# Test Cloud Build Trigger

Before going further we want to make sure your trigger is working. So lets load a sample file into the `content/books` folder. Either copy the examplebook.md renaming to testtrigger.md or create a new file.

`content/books/testtrigger.md`

```markdown
title = "Example Book"
date = 2018-11-26T14:45:13-05:00
images = ["https://res.cloudinary.com/ajonp/image/upload/w_500,q_auto/v1545282630/ajonp-ajonp-com/8-lesson-firestore-functions/bookExample.webp"]
+++

This is a commit test creating a book.

```

## Local git push

Add the new file if needed.

Commit locally

Then push to the remote GitHub repository.

```bash
git push origin
```

## Trigger History

You should now see a history from this trigger listed in [Google Cloud Build History](https://console.cloud.google.com/cloud-build/builds)

![](https://media.codingcat.dev/image/upload/v1657636767/main-codingcatdev-photo/7999bafe-591f-4310-9291-782ebff88536.png)

## Automatically Deployed to Firebase

The last step in cloudbuild.yaml deploys to to firebase, you should see a successful deploy message in your cloud build logs.

```plain text
Starting Step #5
Step #5: Already have image: gcr.io/ajonp-lesson-8/firebase
Step #5:
Step #5: === Deploying to 'ajonp-lesson-8'...
Step #5:
Step #5: i  deploying hosting
Step #5: i  hosting[ajonp-lesson-8]: beginning deploy...
Step #5: i  hosting[ajonp-lesson-8]: found 54 files in public
Step #5: i  hosting: uploading new files [2/48] (4%)
Step #5: ✔  hosting[ajonp-lesson-8]: file upload complete
Step #5: i  hosting[ajonp-lesson-8]: finalizing version...
Step #5: ✔  hosting[ajonp-lesson-8]: version finalized
Step #5: i  hosting[ajonp-lesson-8]: releasing new version...
Step #5: ✔  hosting[ajonp-lesson-8]: release complete
Step #5:
Step #5: ✔  Deploy complete!
Step #5:
Step #5: Project Console: https://console.firebase.google.com/project/ajonp-lesson-8/overview
Step #5: Hosting URL: https://ajonp-lesson-8.firebaseapp.com
Finished Step #5

```

> Reminder if you don't see the new file it may be cached in the browser/service worker, you can force refresh the browser to see this.

# Fork/Clone lesson-8-firestore-functions

The [lesson-8-firestore-functions](https://github.com/AJONPLLC/lesson-8-firestore-functions) is setup to be the admin side of the site that will interact with [Firebase Firestore](https://firebase.google.com/docs/firestore/). I wrote this in Angular, but you could use any Web framework, iOS, Android, Unity...

## Install npm dependencies

Verify that you are in the base directory

```plain text
npm install
```

## Serve locally

At this time you are still pointing at the AJONPLLC project database so you will see some books listed. You must switch to your new firebase project that we created above by changing.

## Update the firebase configuration

> 

	You only need one environment file, but I often have a dev and production setup with both.

In your Firebase Project Overview there is a gear for Settings->Project settings.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/6b68f226-65bf-40dd-943a-a9bbc435e747/msoukza2h7qt3caep5op.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=48cf2d853b62b60b61654752d4b972d96e3123f1273e0e6c251f3394039bf08e&X-Amz-SignedHeaders=host&x-id=GetObject)

Then select the "Add Firebase to your web app" under the "Your apps" section.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/fbfba08e-b753-4067-9892-68ff3e924651/dt6rddue4ra5oezt4fmi.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=247f47e7c46a23f06ce694a185f889a6e2c0d0d094cdc82d5d185acb361f62f5&X-Amz-SignedHeaders=host&x-id=GetObject)

Copy the Javascript object that is assigned to config, in the next step we will paste this into our environment files.

`src/environments/environment.ts and src/environment/environment.prod.ts`

```javascript
{
    apiKey: "your-apiKey",
    authDomain: "your-project.firebaseapp.com",
    databaseURL: "https://your-project.firebaseio.com",
    projectId: "your-project",
    storageBucket: "your-project.appspot.com",
    messagingSenderId: "your-messagingSenderId"
}

```

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/5aed7a8b-9a7c-4096-a6f0-a5fb1fee5eb8/k4oljh0egz3ajrt9reo8.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=3d1bc57af057341727a32a9882dcb3a223670d7a386999bab334f87f9020dfa4&X-Amz-SignedHeaders=host&x-id=GetObject)

## Authentication

Now because this is a new project that we are directing this app towards, you will most likely see an error if you go back to `http://localhost:4200` as we have not updated our Authentication settings yet.

You can update this in Authentication -> Sign-in method. Edit the Google Sign-in provider and enable it. You can find more in the [Firebase Authentication Docs](https://firebase.google.com/docs/auth/)

## Firestore database

Back in your Firestore project select Database->Create Database

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/257c3b51-e455-43b7-84e4-cea3d4e18cc8/ni4xs9bhwfsexfdf6jho.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=e82555195a62fa1b3572b5ffd519d9c48a51f1946dc32cf0c7be8b4880b130ad&X-Amz-SignedHeaders=host&x-id=GetObject)

I recommend always starting in a lock mode, it helps you understand what security you will need throughout the app without forgetting something later. At times it even helps with you Data Model as some setups on security are just terrible.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/946dc2fd-8def-4427-ba4a-872ad2bbb8c4/qcgjr0ld7j9n9vyqji1n.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=62289a4c0a8578538871ccdd1e385d4d79ca0e4cf307c2ad4075545bd113ef08&X-Amz-SignedHeaders=host&x-id=GetObject)

> Note at this point because your project is in lock mode you will not be able to successfully update the Firestore database and you will see failures, as every login we update a users record.

## Firebase Hosting Updates

### Update the project name everywhere

You can do a full project find and replace looking for `ajonp-lesson-8-admin` and replace with your-name.

Example I changed mine from `ajonp-lesson-8-admin` to `ajonp-lesson-8-admin2`. If you use VSCode you can do it like below.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/fee2e1df-9f8e-47a2-b7b0-5998455887c4/fqslxxgc9jyelgl81xkq.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=793f71076ce8fcaadc9233144bc2d2877531fb607e64d31b4e6fd29e4a6c961a&X-Amz-SignedHeaders=host&x-id=GetObject)

src/styles/ajonp-lesson-8-admin -> your_name

If you get an error that looks similar to this, it is because the styles file was changed and we just switched all the references in the line above.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/7a65fa80-c9ec-498f-9330-25d8f8cbe664/hpldbf8rxw9a4f4c2ztj.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=62caf3287cb97c1352c894afd44515694cc371ea0981674f246d332870b35430&X-Amz-SignedHeaders=host&x-id=GetObject)

I changed mine from `ajonp-lesson-8-admin-app-theme.scss` to `ajonp-lesson-8-admin2-app-theme.scss`.

> 

	At this time you can test adding and deleting books from the database, but we still have plumbing to work on getting these to build the Hugo site.

# Build Angular Project

At this time I have not started using the Ivy rendering engine, but I hope it can complete builds faster in the future. Grab a quick coffee when you run this one.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/9ccad64b-ffff-4bee-82ed-9c1aec94e0c7/jzryeqykv2bhtzqvyvem.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=578f6a74bf2f24f3ba8f9bcbf3792b6c03b456a073e71840a860c8c3018193e3&X-Amz-SignedHeaders=host&x-id=GetObject)

Now your entire site is ready for hosting, you can try it locally but running `firebase serve`.

# Deploy Firebase Hosting from CLI

Now that we have a production Angular build in `dist/ajonp-lesson-8-admin2` we can deploy this out to our Firebase Hosting, we just need to create a new site that is authorized.

## Create new site

Hosting->Dashboard->Advanced select "Add another site"

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/641f7ec9-30ba-458e-958f-6a42600575c7/bmvevp4ss4g85fnax9jc.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=26c0488176b60d4de7a44eb14c3c970197f581bfe3690c28e8443bffe020363e&X-Amz-SignedHeaders=host&x-id=GetObject)

## Update Authorized Domains

Because we are serving this from a different domain than normal you will need to add it to our Authorized Domains. You can find this in Authentication->Sign-in method->Authorized domains, select "Add domain".

This will be `your-project-admin`.firebaseapp.com

You should now see three localhost, default (our Hugo site), your-project-admin site.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/01fa9af3-7c02-4af4-add9-cf7fdce6efc8/tjwkte0y4bcml9whonre.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=526d5e43fcb19c106825df8f763690d4276f57f3cceabe162b4c29064cb55802&X-Amz-SignedHeaders=host&x-id=GetObject)

# Update Firestore Trigger to match your GitHub repo

Now that we have both a Hugo site up and running and an Angular admin site up and running we can setup a Firebase Function to trigger everytime we add/delete a book.

## Create Your Github Personal Token

You will need to [Create Github Personal Token](https://github.com/settings/tokens), otherwise you will be sending your password for each request. You can find this in settings->Developer Settings->Personal Access Tokens, select "Generate New Token". This is like a password but you can restrict what access the token is allowed.

For this you can give the repo and read:user access, Name it something meaningful.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/c4b07642-f256-4f56-8f1d-2341c600c69d/suutevgobgp1hsgvja3k.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=ee1e2fe818cba59ac5bf8a87849009e74c17aef8114b1bcd7afb659454679f70&X-Amz-SignedHeaders=host&x-id=GetObject)

## Add GitHub Personal Token to Firebase Functions

You can not run this command to save your token out where only Firebase Functions will have access. In a future lesson I am going to show how to secure these better.

You should see a message like this.

# Deploy Firebase Functions from CLI

Now Deploy your functions, this will transcode the typescript and put the js files in the lib directory. I have both of these set to run on Node 8.

You should see a success message

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/e7a5166d-3e58-4d97-9517-8fd2c325cf2e/mneaigflqyxmqoqp557b.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=0854728c1bea736f0f164c5f221b77a51db7942787f579d5b706195f021c697f&X-Amz-SignedHeaders=host&x-id=GetObject)

# Add a book, watch it show in Hugo site

> Warning you could have a race condition occur where your first trigger to build happens after your last. There are a few work arounds for this that I don't cover in this lesson. Remember everytime you save a build happens in Google Cloud Build to regenerate your entire Hugo site.

In the bottom right corner you can click the + fab button, this will open the form for a New Book.

![](https://s3.us-west-2.amazonaws.com/secure.notion-static.com/9dc2c538-c904-43de-b78f-20fdca7f0f58/xyn381bujrehp021yeju.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20220830%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20220830T192804Z&X-Amz-Expires=3600&X-Amz-Signature=be20e8bfb503947d3df593854e8d25e953541f316dc47fad60553b8549dcbd68&X-Amz-SignedHeaders=host&x-id=GetObject)

Once you click save this will trigger the Cloud Function `gitBookCreateHugoCommit`. Checkout the [logs](https://console.cloud.google.com/logs/viewer)

Watch for the build to take place in [Google Coud Builder History](https://console.cloud.google.com/cloud-build/builds)

You can then navigate over to your Hugo site (after about 2 minutes) and see the new book.

## Delete a book

This does the same thing as adding except it will trigger the `gitBookDeleteHugoCommit` Cloud Function.