Use Firestore to Build Hugo Content

Lesson 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
- 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 Dashboard create a new Budget for your project incase you try this more than say 50 times.
Fork/Clone lesson-8-hugo
Clone your Fork

git clone https://github.com/<your_name>/lesson-8-hugo.git && cd lesson-8-hugo
Test serving hugo locally
git submodule init && git submodule update --remote
hugo server -t ajonp-hugo-ionic --config config.toml
http://localhost:1313/
.
http://localhost:1313/books/
.
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 onhttp://localhost:1313/books/
.
Create Hugo firebase hosting site
Create firebase project


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

Update local firebase files
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.{
"projects": {
"default": "ajonp-lesson-8"
},
"targets": {
"ajonp-lesson-8": {
"hosting": {
"ajonp-lesson-8-hugo": [
"ajonp-lesson-8-hugo"
]
}
}
}
}
{
"projects": {
"default": "your-project"
},
"targets": {
"your-project": {
"hosting": {
"your-hosting-name": [
"your-hosting-name"
]
}
}
}
}
{
"hosting": {
"target": "ajonp-lesson-8-hugo",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
{
"hosting": {
"target": "your-hosting-name",
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
firebase deploy
If you only see a few check that you ran the submodule update.

Create Hugo Content Google Cloud Build Trigger
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.

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

Google Cloud Trigger Enable


If you receive an error, please make sure that you have changed to a Blaze Plan in Firebase.
Google Cloud Trigger Create


- Name: Hugo CI/CD
- Branch: master
- Build configuration: cloudbuild.yaml
- Substitution variables: _FIREBASE_TOKEN = yourtoken.
Test Cloud Build Trigger
content/books
folder. Either copy the examplebook.md renaming to testtrigger.md or create a new file.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
git add .
git commit -m "Trigger Commit"
git push origin
Trigger History
You should now see a history from this trigger listed in Google Cloud Build History

Automatically Deployed to Firebase
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
Install npm dependencies
npm install
Serve locally
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.

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

{
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"
}

Authentication
http://localhost:4200
as we have not updated our Authentication settings yet.
Firestore database


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 deploy --only firestore:rules
Firebase Hosting Updates
Update the project name everywhere
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.

Update Styles

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
ng build --prod
Results

firebase serve
.Deploy Firebase Hosting from CLI
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
firebase deploy --only hosting
Results

Update Authorized Domains
your-project-admin
.firebaseapp.comYou should now see three localhost, default (our Hugo site), your-project-admin site.

Update Firestore Trigger to match your GitHub repo
Create Your Github Personal Token
For this you can give the repo and read:user access, Name it something meaningful.

Add GitHub Personal Token to Firebase Functions
firebase functions:config:set git.token=your-token
You should see a message like this.

Deploy Firebase Functions from CLI
firebase deploy --only functions
You should see a success message

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.

gitBookCreateHugoCommit
. Checkout the logs
Delete a book
gitBookDeleteHugoCommit
Cloud Function.