I want to automatically update Gitea

5 min

language: ja bn de en es fr hi ko pt ru zh-cn zh-tw

Hello, I'm incompetent.
I made a typo again today, but I'll leave it as is.

Basically, I don't want to mess with the Git server too much or think about operational aspects, so I want it to update automatically.
In fact, it's both a good and bad point of Gitea that it simplifies both the frontend and backend into a single binary.
So, all I need to do is pull the binary.

How to stably store the binary

So, I want to store the binary stably, and it seems I can get it from Releases. Releases · go-gitea/gitea · GitHub
In other words, it seems I just need to pull the download URL from here.

Trying to hit the API

The download URL can be obtained as follows.

curl https://api.github.com/repos/go-gitea/gitea/releases/latest | jq -r '.assets[].browser_download_url'

Ideally, I'd like to further narrow it down with jq from here, but looking at the json key:value, it doesn't seem possible, so it looks like I'll just have to grep for the target binary afterwards. *1


*1 After being told by hymkor (HAYAMA_Kaoru) · GitHub that it shouldn't be impossible, I remembered using jq at work recently and was able to extract only the target object as follows.

curl https://api.github.com/repos/go-gitea/gitea/releases/latest | jq -r '.assets[] | select(.name | match("linux-amd64$")) | .browser_download_url '

Or

curl https://api.github.com/repos/go-gitea/gitea/releases/latest | jq -r '.assets[] | select(.browser_download_url | match("linux-amd64$")) | .browser_download_url '

So, saying it couldn't be done was a mistake (tehepero)
Please consider the rest as my ramblings.


So, I did this.

$ curl https://api.github.com/repos/go-gitea/gitea/releases/latest | jq -r '.assets[].browser_download_url' | grep linux-amd64$ 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  171k    0  171k    0     0   291k      0 --:--:-- --:--:-- --:--:--  291k
https://github.com/go-gitea/gitea/releases/download/v1.23.4/gitea-1.23.4-linux-amd64

Difference from current version

It would be a waste of traffic to deploy a new binary every time, so it's necessary to determine this from here.
The easiest way would be to check the current Gitea version and then grep -qi for it.

$ gitea -v | awk '{printf $3}'
1.22.4

Store this in a variable.

Completion

So, it turned out like this.

#!/bin/bash

# give me root

BINARY_URL=$(curl https://api.github.com/repos/go-gitea/gitea/releases/latest |\
  jq -r '.assets[].browser_download_url' |\
  grep linux-amd64$ )

NOW_GITEA_V=$(gitea -v | awk '{printf $3}')
DAEMON_RESTART="service gitea restart"

if ! echo $BINARY_URL | grep -qi $NOW_GITEA_V ; then
  wget -O gitea $BINARY_URL
  chmod +x gitea || exit 2
  mv gitea /usr/local/bin/gitea
  $DAEMON_RESTART 
else
  exit 0
fi

If I put this in root's crontab, it will run as a periodic script.
In the first place, if chmod fails, it will exit 2 and quit, so it's unlikely to behave strangely.

Despite saying Gitea is too rich in features, I find myself using it, which makes me feel a bit inadequate.

Related Posts