Clever link to latest Hugo version download?

Hi everyone. I have a documentation page instruction my users on how to set up the Hugo environment for their contributions:

wget https://github.com/gohugoio/hugo/releases/download/v0.36/hugo_0.36_Linux-64bit.deb

Now, you can see that is outdated, but I don’t want to have to change this every time there is a Hugo release. Does any one know of a clever way to link to the latest Hugo release for Ubuntu 64-bit?

How can I get the bit that should read 0.41 at the moment?

Answering myself, I found out that GitHub keeps a tag called latest so here’s a clever (if I may say so) bash thing to get the latest Hugo for Ubuntu 64bit:

wget https://github.com`wget -qO- https://github.com/gohugoio/hugo/releases/latest | grep -oE -m 1 ''\/gohugoio\/hugo\/releases\/download\/v[0-9]+.[0-9]+\/hugo_[0-9]+.[0-9]+_Linux-64bit.deb'`

Interesting things for geeks:

  • the inner wget uses the -qO- option to redirect downloaded output to stdout so I can handle it directly by piping, without writing to any file on disk.
  • so that gives me a GitHub HTML page which I can parse for the first (-m 1) reference to a 64bit deb package. That will be the latest.
  • grep -oE uses a regular expression to get what I want, and tells grep to give me just that part of the line
  • the outer wget concatenates this path+filename with the domain and downloads the file

Of course, I knew none of these things ten minutes ago, one learns as one goes along :slight_smile:

EDIT: improved regular expression to match version numbers in an even cleverer fashion

6 Likes

Nice work.

While that will work for now, after bep has been busy, it will eventually fail :wink:

Releases > v9 such as 10.01 or any with more than 99 point releases such as 0.100 would fail.

I will add a separate thread to show how to do the same thing using PowerShell for Windows users.

Thanks for pushing me to improve! :slight_smile: - I’ve edited the expression to match more version numbers.

For anybody interested in the Powershell version, here’s a link

1 Like

There was an extra apostrpohe breaking the command I gave above. Also, it wasn’t able to handle the minor versions, like the latest 0.47.1.

So here is a new command with both of these issues fixed:

wget https://github.com`wget -qO- https://github.com/gohugoio/hugo/releases/latest | grep -oE -m 1 '\/gohugoio\/hugo\/releases\/download\/v[0-9]+.[0-9]+.[0-9]*\/hugo_[0-9]+.[0-9]+.[0-9]*_Linux-64bit.deb'`

If you want the latest extended Sass/SCSS version use this one instead:

wget https://github.com`wget -qO- https://github.com/gohugoio/hugo/releases/latest | grep -oE -m 1 '\/gohugoio\/hugo\/releases\/download\/v[0-9]+.[0-9]+.[0-9]*\/hugo_extended_[0-9]+.[0-9]+.[0-9]*_Linux-64bit.deb'`

There’s been a few changes in the format of the release links since the last post here, but the following should download the latest release of April 2023.

Replace windows-amd64.zip with your preferred release, and be mindful about the extension of the output:

curl -s --silent -N "https://api.github.com/repos/gohugoio/hugo/releases/latest" | jq '.assets[] | select(.name | contains("windows-amd64.zip"))' | jq .browser_download_url | xargs wget -qO- > hugo-latest.zip

or use the following for the extended version:

curl -s --silent -N "https://api.github.com/repos/gohugoio/hugo/releases/latest" | jq '.assets[] | select(.name | contains("windows-amd64.zip") and contains("extended"))' | jq .browser_download_url | xargs wget -qO- > hugo-latest.zip

or to extract the executable directly:

curl -s --silent -N "https://api.github.com/repos/gohugoio/hugo/releases/latest" | jq '.assets[] | select(.name | contains("windows-amd64.zip") and contains("extended"))' | jq .browser_download_url | xargs wget -qO- | zcat > hugo.exe

The last one is not bulletproof: it can fail if you go over the GitHub API limit (60 calls every hour) or if hugo.exe is somehow not the first file in the archive.

1 Like