moddedbear.com

How to Post to a Hugo Blog on Android

A few months back I went looking into whether it was possible to post to my Hugo blog here using only my Android phone.

It turns out that it is! It may even be slightly more straightforward than you’d guess, but I still ran into enough snags during the setup that I figured it’d be useful to document.

What you’ll need

Setup Termux

If you haven’t used Termux before, one of the first things you’ll want to do is run termux-setup-storage. This will grant Termux access to your phone’s storage, which we’ll need later in order to clone our site somewhere where our text editor will be able to get to it.

You’ll also need to install git with pkg install git and Hugo with pkg install hugo.

Setup git

There’s one option that you’ll need to add to your git config in Termux, otherwise you’ll probably run into errors later on about an “unsafe repository”. From what I can tell, this has something to do with how Android or Termux is handling file ownership, though much more than that I don’t know.

You can get around this error by running git config --global --add safe.directory '*'. If you want to read up on why this safe directory check is in place, you can check the git documentation and this commit for more information. As long as you trust the files on your phone’s storage (I can’t think of a good reason why you wouldn’t), you should be safe adding this config option to bypass the safety check.

Clone your site

This step will obviously vary slightly depending on where you host your site’s repository. In my case, the git repository for my site is hosted on GitHub, and my preferred way of accessing it is through GitHub’s command line tool. Fortunately this is easy to install in Termux with pkg install gh. Once it’s installed, you should be able to authenticate normally with gh auth login.

Termux normally starts in its own internal directory which other apps can’t access, so you’ll need to navigate to somewhere in Android’s “external” storage where Markor or your chosen text editor will be able to make edits. On my Google Pixel, this is located at /storage/emulated/0/. Termux will only be able to access this if you’ve run the termux-setup-storage command from earlier.

You can clone your site anywhere under that directory that makes sense to you. For me, that was in the Documents directory.

Get ready to write

If you’re used to creating a new post with the hugo new command, you might notice that it fails with an error about being unable to obtain a build lock. This is because some commands cause Hugo to try to obtain an OS-level write lock on the .hugo_build.lock file, which here it’s unable to do either because of Android or Termux. My workaround is to create the new file manually and copy the frontmatter metadata from an existing post.

The hugo server and hugo build commands will also fail for the same reason, but these allow the build lock to be skipped by adding the --noBuildLock flag.

There’s a decent explanation of why this build lock exists on the Hugo support forum if you’d like to learn more about it. As long as you’re not running multiple instances of Hugo at the same time, I don’t believe you’ll run into issues disabling it.

Write and publish

Everything past this point should work as you’d expect it to. Just edit your post in Markor or whichever text editor you’ve chosen and publish it when you’re ready. You can even preview your site with hugo build (you may need the --noBuildLock flag mentioned earlier) and it works just as you’d expect it to.

My site gets built and deployed by Vercel automatically when I push to the main branch, so to publish all I have to do is create a new commit and push it. If your publishing process involves building locally then copying the files somewhere, I assume that would also work as expected since common tools like scp are available in Termux.

Final thoughts

Everything here is based on my experience with my Google Pixel running Android 15. The process should be identical or at least extremely similar on other Android phones. If you try this and find that it isn’t, feel free to reach out to me with the details and I’ll try to get them added!

— JP

#android #blogging #guides #hugo

↪ Reply

↪ Comment via Fediverse