I’d gotten this working earlier and wanted to post it up.
And then promptly forgot about it.
Until today that is, when I wanted to make changes to it.

Here it is for posterity.
The newest change is that the action now runs every six hours.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
name: Build and Deploy Janusworx website
on:
  schedule:
    - cron: '0 */6 * * *'
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: intel # make sure there are runners tagged "intel" on your network or replace this with something of your own

    env:
      HUGO_VERSION: ${{ vars.HUGO_VERSION }}
    steps:  
      - name: Install wget & rsync
        run: apt-get update && apt-get install wget rsync -y

      - name: Install Hugo
        run: |
          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
          && dpkg -i ${{ runner.temp }}/hugo.deb
      
      - name: Checkout the website repo
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
      
      - name: Build website with Hugo
        env:
          HUGO_ENVIRONMENT: production
          TZ: Asia/Kolkata
        run: |
          hugo \
            --gc \
            --minify
            
      - name: Install SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
          chmod 644 ~/.ssh/known_hosts

      - name: Deploy website with rsync
        env:
          REMOTE_HOST: ${{ vars.REMOTE_HOST }}
          REMOTE_USER: ${{ vars.REMOTE_USER }}
          REMOTE_PATH: ${{ vars.REMOTE_PATH }}
        run: |
          rsync -avz --delete public/ $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH

Notes:
It needs a few variables set, in the repo (repo->Settings->Actions->Variables), which are the details I need (Hugo version, Remote Host IP, Remote path to copy the built output to, and the Remote user to use for SSH) to push via rsync to the host.
Hugo version is in there, because Hugo breaks things all the time, so I upgrade cautiously.

It also needs a couple of secrets, set in the repo (repo->Settings->Actions->Secrets) to enable rsync to copy. (SSH_KNOWN_HOSTS and SSH_PRIVATE_KEY). Also best if this is a dedicated private key just for copying. Running ssh-keyscan "your-host-name-or-ip" > known_host_data.txt should dump the data you’d need for SSH_KNOWN_HOSTS

With the changes I made today, the workflow can publish

  • on demand
  • every 6 hours
  • every time I commit to the repo

I added time based publishing, so that I could publish future dated posts as and when the time rolls around.
With this, I think I’m done tinkering with my blog, with the exception of moving it out of my home network and out on to my VM, so that I can truly publish from anywhere. That is a big heft for me currently and a job for future Jason.


Feedback on this post?
Mail me at feedback at this domain or continue the discourse here.

P.S. Subscribe to my mailing list!
Forward these posts and letters to your friends and get them to subscribe!
P.P.S. Feed my insatiable reading habit.