Build & Deploy Hugo Site With Sourcehut
Requirements:
SSH keys
First, create a designated pair of SSH keys for the job:
ssh-keygen -t ed25519 -f ~/.ssh/srht
Two keys will be created: ~/.ssh/srht with private key, and ~/.ssh/srht.pub with public key.
Add private key to Sourcehut
Go to Sourcehut Secrets and add new secret: paste the contents of ~/.ssh/srht into the form, selecting SSH Key.
Copy the generated UUID of added key (should be looking like 49f119e5-0291-44ac-b79e-gf483b7d2fa1).
Sourcehut will also store the private key on its server (~/.ssh/UUID).
Add public key to Hetzner
Open Hetzner KonsoleH, look for Public SFTP Keys section, and add the public key (~/.ssh/srht.pub).
For additional security, Password-Login option can be disabled in Hetzner SFTP settings. Only hosts with added SSH keys will be able to connect to the server. Additional FTP users will be disabled too.
Build manifest
Create .build.yml file in the root of your Hugo website repository, and change the settings accordingly:
image: alpine/edge
packages:
- hugo
- rclone
sources:
- git@git.sr.ht:~USERNAME@REPOSITORY # change to your Sourchut repository
secrets:
- XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX # Sourcehut Secret UUID
tasks:
- build: |
cd REPOSITORY
hugo --gc --cleanDestinationDir --minify
- deploy: |
cd REPOSITORY
rclone sync \
--sftp-host HETZNER-SERVER.COM \
--sftp-user HETZNER-FTP-USER \
--sftp-key-file '~/.ssh/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX' \
public/ \
:sftp:public_html/
Run a test build
Now you can submit the build manifest from the web to check the configuration. Open builds.sr.ht/submit and paste the contents of .build.yml into the text field. Set Visibility to Private, and click Submit.
With correct settings Sourcehut will:
- Start a virtual machine with Alpine Linux
- Install hugo and rclone
- Clone your private repository
- Build static website with Hugo
- Deploy the contents of public/ to Hetzner server via SFTP
Check logs for possible errors. Sourcehut will also allow to SSH into the virtual machine for troubleshooting.
Commit and push
“git.sr.ht will automatically submit builds for you if you store a manifest in the repository as .build.yml. Each time you push, a build with this manifest will be submitted,” — https://man.sr.ht/builds.sr.ht/#gitsrht
Open builds.sr.ht right after the commit to see the building process in real time.
Even though Sourcehuts Builds are available for non-paid accounts, consider subscribing to one the paid tiers (starting at only $2/month) to support the serivce.
Reply via Email or Mastodon