Installing Gitea on Linux and Configuring CI/CD with FTP Deployment

How to install and configure Gitea, an open-source Git service, for managing source control and setting up automatic deployment (CI/CD).

By Tim TrottSoftware Engineering • February 21, 2023
1,339 words, estimated reading time 5 minutes.
Installing Gitea on Linux and Configuring CI/CD with FTP Deployment

Gitea is an open-source Git service written in Go, lightweight, and easy to install. This guide will walk you through installing Gitea on a Linux server, creating your first repository, checking in some sample code, and configuring runners for automatic deployment to a web server via FTP.

Version control, also known as source control, is a pivotal system that manages changes to a project's files, fostering collaboration among multiple contributors. Git, a widely used distributed source control system, tracks changes, enables branching and merging, and promotes collaboration by allowing developers to work on different project parts simultaneously without overwriting each other's work. It provides a comprehensive history of changes, aiding in identifying when and why changes were made and supporting reverting to previous versions if needed.

Prerequisites for Installing Gitea

  1. A Linux server (Ubuntu 20.04 LTS is used in this guide)
  2. An instance of SQLite, PostgreSQL, or MySQL/MariaDB
  3. Root or sudo access to the server
  4. Basic knowledge of command-line operations
  5. FTP credentials for your web server

Installing Gitea

First, ensure your system is up-to-date by issuing the 'update' command to refresh the package list and the 'upgrade' command to install the latest versions of the packages.

bash
sudo apt-get update && sudo apt-get upgrade

Install Required Dependencies

The first step is to install Git on your server, and we will also need wget to download Gitea.

bash
sudo apt install -y git wget

Once complete, you can verify git by running the command git --version, which should output the installed version number.

Creating a Gitea User, which will be used for git access, is a straightforward process that you can confidently navigate.

bash
sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git

This will create a new user called git with a home folder /home/git. By including --disabled-password, we turn off login, which only the system can use.

Download and Install Gitea

Next, we can download the latest Gitea binary from the repository. When this article was written, version 1.15.7 was the latest available. You should check the Gitea website for the latest version and change the command below accordingly.

bash
wget -O /tmp/gitea https://dl.gitea.io/gitea/1.15.7/gitea-1.15.7-linux-amd64

This will download the latest version of Gitea from their servers and put it into the temp directory on your server. We then need to move this binary to a more suitable location and change the permissions to execute it.

bash
sudo mv /tmp/gitea /usr/local/bin/gitea
sudo chmod +x /usr/local/bin/gitea

Next, we must create the required directories for Gitea and set the permissions for the git user we created above.

bash
sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R git:git /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/

sudo mkdir /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

We need Gitea to run as a service. Otherwise, you will have to run it each time manually. To run Gitea as a service, we need to download and add the service file to the running services. We will then create the config file, which will allow Gitea to run.

bash
sudo wget https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/systemd/gitea.service -P /etc/systemd/system/

Reload the systemd daemon and start Gitea:

bash
sudo systemctl daemon-reload
sudo systemctl enable --now gitea

Next, we can verify that Gitea is running using the command sudo systemctl status gitea.

If you use the UFW firewall, you must allow the Gitea default port to accept incoming connections using sudo ufw allow 3000/tcp.

Configure Gitea

Open your web browser and navigate to http://your_server:3000 to access the Gitea setup page. Follow the instructions to complete the setup. Most of the options are self explanatory. For the database type, we recommend using MySQL or MariaDB. SQLite should only be used for very small installations, such as a single user server. For the username, use git as we created earlier. Be sure to set the domain name away from localhost; otherwise, you will encounter issues browsing the interface on the network.

Once this setup page is submitted, you can register to create a new user. The first user is always the admin account.

Create Your First Repository

Navigate to your Gitea instance and log in with your user credentials.

Create a new repository by clicking the `+` icon at the top right corner and selecting 'New Repository'. Fill in the repository details, such as the name and description, and click 'Create Repository'.

Creating a new repository in Gita
Creating a new repository in Gita

Clone the new repository to your local machine using git (or your favourite git GUI). When you create the repository, Gitea will show you the URL to use here; you only have to copy and paste it.

bash
git clone http://your_server_ip:3000/username/repository_name.git

Navigate to the cloned directory and add some sample code:

bash
cd repository_name
echo "Hello World!" > README.md

Commit and push your changes to the Gitea repository:

bash
git add README.md
git commit -m "Initial commit"
git push origin main

Configure Runners for Automatic Deployment

For this article, we are going to use Drone CI to set up automatic deployments to a web server via FTP. Other task runners are available for Gitea.

To use Drone in Gitea, we must first create an OAuth2 application within Gitea. This is done in the Settings > Applications. We can create an OAuth2 application from this page and generate access tokens. These will be used later. We also need to generate an RPC shared secret, which will be exchanged between the Drone server and runner.

bash
openssl rand -hex 16

Make a note of this, as we will need it later.

Creating an OAuth2 credentials in Gitea
Creating an OAuth2 credentials in Gitea

Drone CI runs in a docker container, so first, we must install docker (if you don't already have it installed).

bash
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

Now, we can create a Docker network for Drone:

bash
sudo docker network create drone

Start the Drone server using this command, substituting the client ID and secret from the OAuth2 app we created in Gitea earlier.

bash
sudo docker run -d --network=drone \
  --name=drone-server \
  --volume=/var/lib/drone:/data \
  --env=DRONE_GITEA_SERVER=http://your_server_ip:3000 \
  --env=DRONE_GITEA_CLIENT_ID=your_gitea_client_id \
  --env=DRONE_GITEA_CLIENT_SECRET=your_gitea_client_secret \
  --env=DRONE_RPC_SECRET=your_rpc_secret \
  --env=DRONE_SERVER_HOST=your_server_ip \
  --env=DRONE_SERVER_PROTO=http \
  --publish=8080:80 \
  --restart=always \
  drone/drone:1

Now we start the Drone runner itself:

bash
sudo docker run -d --network=drone \
  --name=drone-runner \
  --volume=/var/run/docker.sock:/var/run/docker.sock \
  --env=DRONE_RPC_PROTO=http \
  --env=DRONE_RPC_HOST=your_server_ip:8080 \
  --env=DRONE_RPC_SECRET=your_rpc_secret \
  --env=DRONE_RUNNER_CAPACITY=2 \
  --env=DRONE_RUNNER_NAME=runner \
  --publish=3000:3000 \
  --restart=always \
  drone/drone-runner-docker:1

Next, we need to configure Drone CI in Gitea.

  1. Log in to Gitea and go to your repository.
  2. Click on `Settings` and then `Webhooks`.
  3. Add a new webhook with the following URL: `http://your_server_ip:8080/hook`.
    • The port should match the configuration for DRONE_RPC_HOST above.

Create .drone.yml configuration file in your repository with the following content:

yaml
kind: pipeline
type: docker
name: default

steps:
- name: deploy
  image: appleboy/drone-ftp
  settings:
    host: ftp.yourserver.com
    username: your_ftp_username
    password: your_ftp_password
    source: ./public
    target: /path/on/server

Commit and push the configuration file to the repo:

Commit and push the `.drone.yml` file:

bash
git add .drone.yml
git commit -m "Add Drone CI configuration"
git push origin main

When files are committed to your repository, the runner will deploy them to the FTP server.

Conclusion

You have successfully installed Gitea on a Linux server, created your first repository, checked in some sample code, and configured runners for automatic deployment to a web server via FTP. This setup will streamline your development workflow, allowing for continuous integration and deployment with minimal effort.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.