|
| 1 | +--- |
| 2 | +title: "Gitea 🍵" |
| 3 | +date: 2022-08-08T20:03:02+02:00 |
| 4 | +draft: false |
| 5 | +hideLastModified: true |
| 6 | +summaryImage: "img/gitea.webp" |
| 7 | +keepImageRatio: true |
| 8 | +summary: "Setting up your own Git service. And the migration from Alpine to Debian." |
| 9 | +showInMenu: false |
| 10 | +tags: ["Programming", "Git", "Home Lab", "DevOps"] |
| 11 | +--- |
| 12 | + |
| 13 | +## Introduction to Git |
| 14 | + |
| 15 | +Git is currently the dominant version control tool in the market. |
| 16 | +The main idea behind it is to manage the state of various versions of source code. |
| 17 | +A full Git tutorial would be too lengthy for a blog post, so instead, I'll point you to a good resource. |
| 18 | +The comprehensive manual available at: [Pro Git Book](https://git-scm.com/book/en/v2). |
| 19 | + |
| 20 | +Another key feature of Git is that it's a distributed system. |
| 21 | +It allows every user to create their own repository and sync it with others. |
| 22 | +However, it's common practice to have a central server where the main repository is hosted. |
| 23 | +These servers often come with a simple web UI to facilitate interactions with the repository. |
| 24 | +The largest platform for this type of software is [GitHub](https://github.com/), but there are others like [GitLab](https://about.gitlab.com) and [Gitea](https://gitea.io/en-us/). |
| 25 | + |
| 26 | +## My Experience with Gitea 🕰️ |
| 27 | + |
| 28 | +The first service I installed on my home server was Gitea. |
| 29 | + |
| 30 | +It's lightweight, consumes minimal resources, and its web interface is quite user-friendly, making it the perfect choice for me. |
| 31 | +My initial Gitea installation ran inside an LXC container with Alpine Linux. |
| 32 | +Using Alpine’s package manager, installing gitea was straightforward. |
| 33 | + |
| 34 | +However, I aim to create a homogeneous server environment, meaning all servers should run Debian 11 on LXC. |
| 35 | +As a result, the Alpine container had to go, and I needed to migrate my Git server. |
| 36 | + |
| 37 | +Unfortunately, Gitea doesn’t provide a pre-packaged solution for Debian, so the installation is a bit more complex compared to Alpine. |
| 38 | +Gitea is written in [Golang](https://go.dev/), and they provide a static-linked binary that you can download, but the rest requires some manual setup by the admin. |
| 39 | + |
| 40 | +## Installing Gitea on Debian |
| 41 | + |
| 42 | +Here’s a rough outline of how to install Gitea on Debian: |
| 43 | + |
| 44 | +**Update and Install Dependencies:** |
| 45 | + |
| 46 | +{{< codeWide >}} |
| 47 | +apt -y update |
| 48 | +apt -y install git vim bash-completion |
| 49 | +{{< /codeWide >}} |
| 50 | + |
| 51 | +**Add a User for Gitea:** |
| 52 | + |
| 53 | +{{< codeWide >}} |
| 54 | +adduser \ |
| 55 | + --system \ |
| 56 | + --shell /bin/bash \ |
| 57 | + --gecos 'Git Version Control' \ |
| 58 | + --group \ |
| 59 | + --disabled-password \ |
| 60 | + --home /home/gitea \ |
| 61 | + gitea |
| 62 | +{{< /codeWide >}} |
| 63 | + |
| 64 | +**Download the Binary and Move it to `/usr/local/bin:`** |
| 65 | + |
| 66 | +{{< codeWide >}} |
| 67 | +curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep '\linux-amd64$' | wget -i - |
| 68 | +{{< /codeWide >}} |
| 69 | + |
| 70 | +**Change Permissions and Rename the Binary:** |
| 71 | + |
| 72 | +{{< codeWide >}} |
| 73 | +chmod +x gitea-*-linux-amd64 |
| 74 | +mv gitea-*-linux-amd64 /usr/local/bin/gitea |
| 75 | +{{< /codeWide >}} |
| 76 | + |
| 77 | +**Create Directories for Files:** |
| 78 | + |
| 79 | +{{< codeWide >}} |
| 80 | +mkdir -p /etc/gitea /var/lib/gitea/{custom,data,indexers,public,log} |
| 81 | +chown gitea:gitea /var/lib/gitea/{data,indexers,log} |
| 82 | +chmod 750 /var/lib/gitea/{data,indexers,log} |
| 83 | +chown root:gitea /etc/gitea |
| 84 | +chmod 770 /etc/gitea |
| 85 | +{{< /codeWide >}} |
| 86 | + |
| 87 | +**Create a Systemd Unit File:** |
| 88 | + |
| 89 | +{{< codeWide >}} |
| 90 | +vim /etc/systemd/system/gitea.service |
| 91 | +{{< /codeWide >}} |
| 92 | + |
| 93 | +Content of the file: |
| 94 | + |
| 95 | +{{< codeWide >}} |
| 96 | + |
| 97 | +[Unit] |
| 98 | +Description=Gitea (Git with a cup of tea) |
| 99 | +After=syslog.target |
| 100 | +After=network.target |
| 101 | + |
| 102 | +[Service] |
| 103 | +LimitMEMLOCK=infinity |
| 104 | +LimitNOFILE=65535 |
| 105 | +RestartSec=2s |
| 106 | +Type=simple |
| 107 | +User=gitea |
| 108 | +Group=gitea |
| 109 | +WorkingDirectory=/var/lib/gitea/ |
| 110 | +ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini |
| 111 | +Restart=always |
| 112 | +Environment=USER=gitea HOME=/home/gitea GITEA_WORK_DIR=/var/lib/gitea |
| 113 | + |
| 114 | +[Install] |
| 115 | +WantedBy=multi-user.target |
| 116 | +{{< /codeWide >}} |
| 117 | + |
| 118 | +**Start Gitea Service with Systemd:** |
| 119 | + |
| 120 | +{{< codeWide >}} |
| 121 | +systemctl daemon-reload |
| 122 | +systemctl enable --now gitea |
| 123 | +systemctl status gitea |
| 124 | +{{< /codeWide >}} |
| 125 | + |
| 126 | +**Additional Permissions for Gitea** |
| 127 | + |
| 128 | +This is important, or the service won't start properly. |
| 129 | + |
| 130 | +{{< codeWide >}} |
| 131 | +setcap 'cap_net_bind_service=+ep' /usr/local/bin/gitea |
| 132 | +{{< /codeWide >}} |
| 133 | + |
| 134 | +## Migration |
| 135 | + |
| 136 | +The migration involved two steps: backing up the data and the database. |
| 137 | +The data primarily consisted of the repositories, which needed to be copied to the new server. |
| 138 | +You may also need to adjust Linux permissions if the user running Gitea has changed. |
| 139 | + |
| 140 | +In my case, I also had to update Git hooks, as the directory where Gitea was installed had changed. To do this, you can use the following command: |
| 141 | + |
| 142 | +{{< codeWide >}} |
| 143 | +grep -r "/usr/bin/gitea" . | cut -d : -f1 | xargs sed -i 's#/usr/bin/gitea#/usr/local/bin/gitea#g' |
| 144 | +{{< /codeWide >}} |
| 145 | + |
| 146 | +The database was SQLite, and it was easy to dump and restore. |
| 147 | + |
| 148 | +In conclusion, the migration was more difficult than I initially anticipated, |
| 149 | +but it wasn’t rocket science. |
| 150 | +I'm happy to say that I now have Gitea running on Debian 11! 😄 |
0 commit comments