Tyler's Site

Abstract

Git is an extremely valuable tool for devs and admins to be able to manage changes to their configs and code. There are many options for Git hosting, however, many options that people hear about are proprietary with little to no self-hosting options. While there isn’t anything wrong with that per se, self-hosting is a better option in my opinion. So, in this post we are going to look at using Forgejo (a fork of Gitea made by the folks at codeberg.org) to self-host Git services with many of the same features offered by something like GitHub or GitLab. Gitea is another product that would work, however, there have been some disagreements between some of the former Gitea maintainers and the company behind the Gitea project, Gitea Ltd. Additionally, setting up Gitea is well documented on their website, here.

The host for these notes is FreeBSD 14.0-RELEASE-p3 and has an IP address of 192.168.122.11.

Hosting with Forgejo

As of the time of writing, Forgejo is not available via pkg in FreeBSD, but it did not seem particularly difficult to compile myself. Really the only things that are needed to be installed are: Git, Go (version 1.21 or newer), and npm 20. So, here is what the process for installing the dependencies and compiling Forgejo look like:

# Always a good idea to make sure packages are up-to-date
sudo pkg update
sudo pkg upgrade
# You must use go121, as the standard go package is not a new enough version
sudo pkg install npm-node20 go121 git
# Symlinking go121 to go is required to pass the go check and make
# Forgejo recognize that go is installed
sudo ln -s /usr/local/bin/go121 /usr/local/bin/go
# Clone the Forgejo repo
git clone https://codeberg.org/forgejo/forgejo && cd forgejo
TAGS="bindata sqlite sqlite_unlock_notify" make build
./gitea

From here, open a web browser to complete the setup. Navigate to the IP address of the server with the port 3000 (e.g. 192.168.122.11:3000). We can use the daemon utility to run the gitea binary in the background using the following command:

daemon -f ./gitea

Alternatively, we can make a FreeBSD service to be able to manage the application better. This is a very basic service file that is based on the service file from installing gitea on a FreeBSD machine. To create the service file simply run touch /usr/local/etc/rc.d/gitea && chmod +x /usr/local/etc/rc.d/gitea as root, then add the following into the file:

#!/bin/sh
# PROVIDE: gitea
# REQUIRE: NETWORKING SYSLOG
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable gitea:
#
#gitea_enable="YES"

. /etc/rc.subr

name="gitea"
rcvar="gitea_enable"

load_rc_config $name

: ${gitea_user:="git"}
: ${gitea_enable:="NO"}
: ${gitea_facility:="daemon"}
: ${gitea_priority:="debug"}
: ${gitea_shared:="/usr/local/share/${name}"}

command="/usr/local/bin/gitea"
pidfile="/var/run/${name}.pid"
start_cmd="${name}_start"

gitea_start() {
  for d in /var/db/gitea /var/log/gitea; do
    if [ ! -e "$d" ]; then
      mkdir "$d"
      chown ${gitea_user} "$d"
    fi
  done

  daemon -T ${name} \
  -u ${gitea_user} -p ${pidfile} \
  $command --work-path ${gitea_shared}
}

run_rc_command "$1"

From there, add gitea_enable="YES" into /etc/rc.conf, then start the service with service gitea start. The setup will have to be re-done unless the service was pointed to the directory of the initial setup or the files were moved to the new directory. Moving forward with this setup, it will just be important to remember to update the software from time-to-time by running git pull origin master and recompiling from time-to-time (probably at least monthly). If and when there is a ports version or a binary, it would be easier to switch to that so that the software updates with the rest of the OS, but until then, this solution will work.

In the next article I will be going over making a more minimalist Git server using Git daemon.