www.eamoncaddigan.net

Content and configuration for https://www.eamoncaddigan.net
git clone https://git.eamoncaddigan.net/www.eamoncaddigan.net.git
Log | Files | Refs | Submodules | README

index.md (3815B)


      1 ---
      2 title: "2024, Day 7: Publishing posts with a Git remote"
      3 description: "Publishing posts by pushing to my webhost"
      4 date: 2024-12-07T23:41:01-08:00
      5 draft: false
      6 ---
      7 
      8 ## Configuration changes
      9 
     10 Before setting up the server, I made some changes locally in my website
     11 repository that lives on my laptop.
     12 
     13 My Hugo configuration is already set up to use a config directory (cf.
     14 a single file), so I just need to create a new production environment
     15 section.
     16 
     17 In my site home, I created the new file `config/production/hugo.yaml` (most
     18 options live in `config/_default/hugo.yaml`), with the following contents:
     19 
     20 ```yaml
     21 cleanDestinationDir: true
     22 publishDir: '/home/public'
     23 ```
     24 
     25 {{< aside >}}
     26 
     27 I should note that this _specific_ `publishDir` setting works on my server
     28 but was impossible to test on my laptop (even when I created the
     29 `/home/public` directory and made it writable by my user). Hugo seems to be
     30 willing to write to some directories but not others, and I didn’t feel like
     31 tracking down the specific limitations---my guess is that it has to do with
     32 filesystem boundaries, but I don’t want to spend a whole day figuring this
     33 out. I want to call this out as a place where both caution and
     34 trial-and-error are necessary.
     35 
     36 I’m happy with Hugo overall---it’s powerful, fast, and at this point
     37 I understand it moderately well---but this is not an uncommon experience:
     38 I read a description in the documentation, find weird inconsistent behavior
     39 in practice (i.e., things don’t work the way that _I_ infer they will based
     40 on my reading of the docs), and then find a discussion post about the same
     41 issue where the lead developer, @bep, acts dismissive and rude. I’ve never
     42 posted there because I would struggle to remain polite to @bep specifically.
     43 Supporting this project has got to be a thankless task, but his posting
     44 style suggests that he’s not somebody with whom I’d get along.
     45 
     46 {{< / aside >}}
     47 
     48 Now, running `hugo` builds my site the normal way, but running `hugo
     49 --environment production` will add these two additional options to my
     50 configuration; these essentially cause my site to be cleared and rebuilt
     51 when run on my server.
     52 
     53 ## On the server
     54 
     55 1. Clone my website repo to a private directory on my webhost:
     56 
     57 ```bash
     58 cd <private-directory>
     59 git clone <url-of-repo>
     60 cd <repo-name>
     61 ```
     62 
     63 2. Enable “push to deploy” in the repository
     64 
     65 ```bash
     66 git config --local receive.denyCurrentBranch updateInstead
     67 ```
     68 
     69 3. Set up the post-update hook
     70 
     71 Write the following to `.git/hooks/post-receive`, and make the file
     72 executable:
     73 
     74 ```bash
     75 #!/bin/sh
     76 # Build the site IFF main gets updated
     77 
     78 cd ../
     79 while read oval nval ref
     80 do
     81     if [ "$ref" = "refs/heads/main" ]
     82     then
     83         hugo --environment production
     84     fi
     85 done
     86 ```
     87 
     88 This will tell Hugo to build the site whenever it receives a push to `main`.
     89 
     90 I should probably check that `nval` is not `^0{40}$`, which indicates
     91 a deleted branch, but if I’m pushing the deletion of `main` to my server
     92 then things have gotten weird.
     93 
     94 ## Back on my laptop
     95 
     96 I set up a new remote for the repository that lives on my webhost:
     97 
     98 ```bash
     99 git remote add prod ssh://<user>@<host>/<private-directory>/<repo-name>
    100 ```
    101 
    102 Now, any push to the `prod` repo will send my updates to my server and cause
    103 the site to be rebuilt:
    104 
    105 ```bash
    106 git push prod
    107 ```
    108 
    109 ## Why bother?
    110 
    111 [As I said before]({{< relref "/december-adventure/2024-06/" >}}), I already
    112 publish my site by running `git push`, but I used a customized hook in my
    113 local repository, which assumes that I have all my ssh keys set up
    114 correctly, etc. This new setup feels simpler, and if I have to set up a new
    115 computer to publish my site, it’s just a matter of cloning the repository on
    116 my webhost.
    117 
    118 I also learned a bit more about Git and Hugo, which have been major themes
    119 for this part of my #DecemberAdventure.