Build your own code hosting platform based on Gitea


Writing in the front

Why build your own code hosting platform? It all started when I first started working.

I’m in the second semester of my junior year, probably in March 2017, personally made a few small project, receives the school course boring meaningless, finally could not stand the boredom and loneliness of staying in school really (yes, still single dog at that time), skipping classes every day go out and find a job, is the first job.

Think how fast it went, it’s February 2019 (two years of legendary work/paddling experience?). Two companies have changed, and the way we work has changed a lot.

  • The first company period, SFTP a shuttle
  • In the second company, SVN version control wrote hooks that could be automatically pulled after submitting code
  • The third company (now), GitLab, had the team leader control the code merge

My own habits have changed a lot, too. I wrote a PHP script on two servers and requested two PHP scripts to pull the code at the same time by configuring SVN hooks.

After getting familiar with Git Workflow, I am also getting used to using Git to manage my own code. But, like me, I’m sure you don’t always want your repository to be public (github wasn’t free when you first decided to use Gitea), because some things are sensitive.

Let’s compare the various code hosting platforms

  • Self-built Git servergit init --bare test.git) I did at first, but being reasonable was too difficult to manage
  • GitLab, high resource occupancy, give up.
  • Gitee, code cloud, is undoubtedly the best for domestic teams, but the international support is not so good.
  • GitHub, spend money, I’m poor, skip.
  • Gogs, low resource occupancy, easy configuration, support migration, at the beginning of the use of this, personal maintenance, update a little slow.
  • Gitea, Fork version of Gogs, team maintenance, more feature-rich, support for code auditing, more active and aggressive.

At first, I did build my own Git server, which was too inconvenient. Later, I learned about Gogs and started to use Gogs. Later, I learned that Gitea, the Fork version of Gogs, has the same features and more rich functions, so I chose to upgrade to Gitea


Deploy your owngitea

Let’s do it. Let’s get the ball rolling. The rest is dry

The preparatory work

The domain name you intend to use for Gitea is gittest.yiranzai.top

If you want to persist Git and other data, you need to create a directory as a place to store your data

mkdir -p /var/lib/gitea
Copy the code

writedocker-compose.ymlfile

Still not familiar with Docker-compose? Click here to learn how to configure Docker-compose

We need to

  • nginx(using thenginxTo be an external service agent. Don’t letgiteaExpose web services directly to the outside world)
  • memcached(forgiteaprovideCacheService)
  • mysql(storagegiteaData to be stored)
  • gitea(Code hosting platform)
version: "3.7"
services:
  nginx:
    image: nginx:alpine
    container_name: test_nginx
    ports:
      - "80:80"
    restart: always
    networks:
      - giteanet
  mysql:
    image: Mysql: 5.7
    restart: always
    container_name: test_mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root_password
      - MYSQL_DATABASE=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea_password
    networks:
      - giteanet
    volumes:
      - /path/to/conf/my.cnf:/etc/mysql/my.cnf:rw
      - /path/to/data:/var/lib/mysql/:rw
      - /path/to/logs:/var/log/mysql/:rw
  memcache:
    image: memcached:alpine
    container_name: test_memcache
    restart: always
    networks:
      - giteanet
  gitea:
    image: gitea/gitea:latest
    restart: always
    container_name: test_gitea
    networks:
      - giteanet
    ports:
      - "Please"			Git services using port 22 will be more convenient
    volumes:
      - /var/lib/gitea:/data:rw	The gitea data should be persisted and mapped to the host disk
networks:
  giteanet:
Copy the code

Run the service and initialize it

  1. Run the service
$ docker-compose up -d
Creating test_memcache ... done
Creating test_nginx    ... done
Creating test_mysql    ... done
Creating test_gitea    ... done$ docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------------- test_gitea /usr/bin/entrypoint /bin/s ... The Up 0.0.0.0:22 - > 22 / TCP, 0.0.0.0:3000->3000/ TCP test_memcache docker-entrypoint.sh memcached Up 11211/ TCP test_mysql docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp test_nginx nginx -g daemon off; The Up 0.0.0.0:80 - > 80 / TCP $cd/var/lib/gitea $tree. $. ├─ gitGit repository location├ ─ ─ gitea │ ├ ─ ─ the conf │ │ └ ─ ─ app. Ini# gitea configuration file│ └ ─ ─log          # gitea log directory└ ─ ─ SSH# SSH key directory
Copy the code
  1. writenginxThe configuration file
server {
    listen       80;
    server_name gittest.yiranzai.top;
    location / {
        proxy_pass http://test_gitea:3000;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; }}Copy the code
  1. restartnginxMake the configuration take effect
  2. accessgittest.yiranzai.topThe initial configuration is complete

The long graph shows the configuration process

  1. Edit to view gitea’s configuration

See here for Gitea’s complete configuration

$ vim /var/lib/gitea/gitea/app.ini ... [Cache] ADAPTER = memcache HOST = test_memcache:11211Copy the code

Create the warehouse and take off

What? Can’t create a warehouse? Stop stop

Note that the + in the upper right corner can be migrated to another warehouse in Gitea


END

Every time to the end of the inexplicable have some sense of achievement, probably because toss out a new thing.

In short, finally do well, first slowly casually, familiar with this simple and powerful code hosting platform.

Happy Coding!

series

  • Create your own CI/CD workflow based on Gitea+Drone CI+Vault
  • (1) Drone CI For Github — Build their own CI/CD workflow
  • Drone CI uses Vault as credential storage — to build its own CI/CD workflow
  • (3) Lightweight self-built Drone CI For Gitea — to create their own CI/CD workflow
  • Fyi: Build your own code hosting platform based on Gitea