commit 86984a203f23f98a951025cf9fe9b1617f495012
parent fedfcaf5dfa4a6798e3b8fba2c3e0d48adb90c86
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Sat, 7 Dec 2024 23:40:38 -0800
Update the December Adventure log for day 7
Diffstat:
1 file changed, 119 insertions(+), 0 deletions(-)
diff --git a/content/december-adventure/2024-07/index.md b/content/december-adventure/2024-07/index.md
@@ -0,0 +1,119 @@
+---
+title: "2024, Day 7: Publishing posts with a Git remote"
+description: "Publishing posts by pushing to my webhost"
+date: 2024-12-07T23:41:01-08:00
+draft: false
+---
+
+## Configuration changes
+
+Before setting up the server, I made some changes locally in my website
+repository that lives on my laptop.
+
+My Hugo configuration is already set up to use a config directory (cf.
+a single file), so I just need to create a new production environment
+section.
+
+In my site home, I created the new file `config/production/hugo.yaml` (most
+options live in `config/_default/hugo.yaml`), with the following contents:
+
+```yaml
+cleanDestinationDir: true
+publishDir: '/home/public'
+```
+
+{{< aside >}}
+
+I should note that this _specific_ `publishDir` setting works on my server
+but was impossible to test on my laptop (even when I created the
+`/home/public` directory and made it writable by my user). Hugo seems to be
+willing to write to some directories but not others, and I didn’t feel like
+tracking down the specific limitations---my guess is that it has to do with
+filesystem boundaries, but I don’t want to spend a whole day figuring this
+out. I want to call this out as a place where both caution and
+trial-and-error are necessary.
+
+I’m happy with Hugo overall---it’s powerful, fast, and at this point
+I understand it moderately well---but this is not an uncommon experience:
+I read a description in the documentation, find weird inconsistent behavior
+in practice (i.e., things don’t work the way that _I_ infer they will based
+on my reading of the docs), and then find a discussion post about the same
+issue where the lead developer, @bep, acts dismissive and rude. I’ve never
+posted there because I would struggle to remain polite to @bep specifically.
+Supporting this project has got to be a thankless task, but his posting
+style suggests that he’s not somebody with whom I’d get along.
+
+{{< / aside >}}
+
+Now, running `hugo` builds my site the normal way, but running `hugo
+--environment production` will add these two additional options to my
+configuration; these essentially cause my site to be cleared and rebuilt
+when run on my server.
+
+## On the server
+
+1. Clone my website repo to a private directory on my webhost:
+
+```bash
+cd <private-directory>
+git clone <url-of-repo>
+cd <repo-name>
+```
+
+2. Enable “push to deploy” in the repository
+
+```bash
+git config --local receive.denyCurrentBranch updateInstead
+```
+
+3. Set up the post-update hook
+
+Write the following to `.git/hooks/post-receive`, and make the file
+executable:
+
+```bash
+#!/bin/sh
+# Build the site IFF main gets updated
+
+cd ../
+while read oval nval ref
+do
+ if [ "$ref" = "refs/heads/main" ]
+ then
+ hugo --environment production
+ fi
+done
+```
+
+This will tell Hugo to build the site whenever it receives a push to `main`.
+
+I should probably check that `nval` is not `^0{40}$`, which indicates
+a deleted branch, but if I’m pushing the deletion of `main` to my server
+then things have gotten weird.
+
+## Back on my laptop
+
+I set up a new remote for the repository that lives on my webhost:
+
+```bash
+git remote add prod ssh://<user>@<host>/<private-directory>/<repo-name>
+```
+
+Now, any push to the `prod` repo will send my updates to my server and cause
+the site to be rebuilt:
+
+```bash
+git push prod
+```
+
+## Why bother?
+
+[As I said before]({{< relref "/december-adventure/2024-06/" >}}), I already
+publish my site by running `git push`, but I used a customized hook in my
+local repository, which assumes that I have all my ssh keys set up
+correctly, etc. This new setup feels simpler, and if I have to set up a new
+computer to publish my site, it’s just a matter of cloning the repository on
+my webhost.
+
+I also learned a bit more about Git and Hugo, which have been major themes
+for this part of my #DecemberAdventure.