The adventures of Jay
  • About
  • Posts
  • Brews
  • Knacks
  • Links

Selfhosting - Sat, Jul 27, 2024

Bringing git hosting closer to home.

Preface

It’s been almost a year since I said this would be updated regularly. What I realised was my workflow wasn’t great. I had hugo on my laptop but if I wanted to do any work on any other PC, I needed to remote into the laptop. I then had the forehead slapping moment of using git. But then I needed a remote server to host the repo so I could use it across multiple machines. Github or Gitlab is probably the logical choice, but after reading this article, I would rather host it at home.

Options

Find a self-hostable git server with a nice GUI. The two main ones I looked at were Gitlab and Gitea. From what I gathered, Gitlab was more polished, but a heavier system to run. I don’t need that fancy shit. Gitea looked more light weight and I’m still not going to use all the features it offers. I’m just a sparky!

But where do I host it?

I have what others would consider a Homelab, but really it’s just stuff I’ve bought over the years to learn something new or see if I could do something. It turns out this learning methods is very applicable to Industrial Control Systems and Substation Automation Systems (of which I work with both).

In my Homelab I have an HP DL380p Gen 8 which I picked up at an auction a few years ago for $400. In my opinion it was a steal, only uses 120ish watts of power with 8 internal drives and 3 external drives. It’s obviously not working much of those 40 logical cores. Running Proxmox as a hypervisor, it’s helped me learn about virtualisation, networking and in particular PRP (Parallel Redundancy Protocol).

It also allows VMs or containers. Given I haven’t done much with LXC containers, I decided to give it a crack this time. And it’s working sweet!

Get git(ea) goin' get Git(hub) gone!

Installation of the container was a breeze. This time I’ve decided to move away from Ubuntu Server and went with plain old debian. My network is setup to provide DNS with my own domain using the hostname of the machine - so it set ups automagically and I don’t need to configure that. Welcome to http://gitea.bracken.life/ (not publicly accessible).

Migration from Github was (shouldn’t really be surprisngly easy as both solutions use git under the hood, but it was) surprisingly easy. My new found productiivty AI helped me write a bash script which pulled all my repos from Github, saved the to a text file, then told Gitea to clone them.

# Install GitHub CLI (if not already installed):
sudo apt install gh

# Authenticate GitHub CLI:
gh auth login

# List Repos
gh repo list username --json nameWithOwner --limit 100 | jq -r '.[].nameWithOwner' > repos.txt

Then build a script to tell Gitea to clone from Github:

#!/bin/bash

GITEA_URL="http://localhost:3000" # Change this to your Gitea instance URL
GITEA_USER="gitea-username"       # Change to your Gitea username
GITEA_PASS="gitea-password"       # Change to your Gitea password
GITHUB_USER="github-username"     # Change to your GitHub username
GITHUB_TOKEN="github-token"       # Change to your GitHub token

while read repo; do
    REPO_NAME=$(echo $repo | awk '{print $1}' | cut -d'/' -f2)
    echo "Migrating $REPO_NAME..."

    RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null -X POST "$GITEA_URL/api/v1/repos/migrate" \
    -H "Content-Type: application/json" \
    -d '{
        "clone_addr": "https://github.com/'$GITHUB_USER'/'$REPO_NAME'.git",
        "uid": 1,
        "repo_name": "'$REPO_NAME'",
        "auth_username": "'$GITHUB_USER'",
        "auth_password": "'$GITHUB_TOKEN'",
        "mirror": false,
        "private": false
    }' \
    -u $GITEA_USER:$GITEA_PASS)

    if [ $RESPONSE -eq 201 ]; then
        echo "Successfully migrated $REPO_NAME"
    else
        echo "Failed to migrate $REPO_NAME. HTTP response code: $RESPONSE"
    fi
done < repos.txt

Run the script and watch Gitea fill up with all of your Github repo goodness.

Conclusion

So what this now means for me is that my Hugo static site generator repo is hosted on my own Gitea instance running locally in my home. I can update blogs posts from any machine (even remotely {thank you Wireguard}). Hopefully more blogs will appear here on in.

Close-out comments

  • Always secure your network.
  • Only expose to the internet things you don’t mind the world seeing.
  • Always create backups.
  • Follow rules 1 through 3.

Remember, I’m not a programmer, I’m an electrician

Back to Home


hugo.386 theme by Max le Fou | © J Bracken 2024 | Lead for a better world.

GitHub GitLab