Privatization lightweight continuous integration deployment scheme -- 04 private code warehouse service Gitea

Posted by polson on Tue, 08 Mar 2022 10:45:28 +0100

Tip: all notes in this series exist in Github , you can directly Github View all notes

The most popular private code warehouse at the enterprise level is Gitlab. At the beginning, I also planned to deploy Gitlab as a private code warehouse.

However, after the deployment, it is found that Gitlab takes up too much resources. After optimization, it also needs to occupy 3g memory. Finally, we have to give up this scheme.

Later, two lightweight private warehouses, Gitea and Gogs, were found, both of which are excellent lightweight private warehouses.

After comparing them, it is found that Gitea is a project from the Gogs project fork. Officials gave fork reasons: https://blog.gitea.io/2016/12/welcome-to-gitea/ , have a look if you are interested

That is to say, Gitea project is superior to Gogs project, and finally chose Gitea

Although Gitea is lightweight, it also has powerful functions and can be used safely.

https://docs.gitea.io/zh-cn/

Gitea

Database selection

Gitea supports mysql, Postgres, MSSQL, tidb and sqlite3 databases. The simplest is to directly use sqlite3 as a document database without deploying services separately.

postgres database is used here. Now postgres is also the mainstream database. Many enterprises have shifted from mysql to postgress

As for the comparison between Postpress and mysql, interested friends can query by themselves

DockerFile

version: '3.9'
# Create custom network
networks:
  gitea:
    name: gitea
    driver: bridge

services:
  ## Database service
  db:
    image: postgres:latest
    container_name: gitea_db
    restart: always
    networks:
      - gitea # Join gitea network
    ports:
      - 3003:5432
    environment:
      - POSTGRES_USER=gitea # PGSQL default user
      - POSTGRES_PASSWORD=gitea # PGSQL default password
      - POSTGRES_DB=gitea # PGSQL default database
    volumes:
      - /volumes/gitea/db:/var/lib/postgresql/data
  #gitea service
  server:
    image: gitea/gitea:latest
    container_name: gitea_server
    restart: always
    networks:
      - gitea # Join gitea network
    ports:
      - '3000:3000' # HTTP service port
      - '3001:22' # SSH server port
    environment:
      - USER_UID=1000 # The uid and GID used by the running container are used for anonymous data volume mounting,
      - USER_GID=1000 # GID used to run the container
      - APP_NAME=gitea
      - PROTOCOL=http # Access protocol used by the service
      - HTTP_PORT=3000 # The default HTTP listening port is 3000
      - SSH_PORT=22 # SSH port shown in clone URL
      - DOMAIN=82.157.55.94:3000 # HTTP clone URL displayed by UI
      - LANDING_PAGE=explore
      - ROOT_URL=http://82.157.55.94: external URL of 3000 # server
      - DB_TYPE=postgres # Database type
      - DB_HOST=db # Database connection address, using network connection, serverName or containerName
      - DB_NAME=gitea # Database name
      - DB_USER=gitea # Database connection user
      - DB_PASSWD=gitea # Database connection password

      - DISABLE_REGISTRATION=true # Disable user registration. When enabled, only administrators are allowed to add users
      - SHOW_REGISTRATION_BUTTON=false # Show registration button
      - REQUIRE_SIGNIN_VIEW=true # Are all pages accessible only after login

    volumes:
      - /volumes/gitea/server/data:/data
      - /volumes/gitea/server/config:/etc/config
      - /volumes/gitea/server/timezone:/etc/timezone:ro
      - /volumes/gitea/server/localtime:/etc/localtime:ro
    depends_on:
      - db

Two containers are deployed in the Dockerfile configuration file: Postgres (database) service and Gitea service.

Added a custom network using networks. Both containers join the custom network and use the custom network for communication.

PS: Docker provides a network module. Containers belonging to the same network can communicate directly using container name and service name.

The default database, user name and user password information are set in the Postgres service through environment variables.

In the Gitea service, the UID and GID of the running container, the HTTP listening port and the clone URL displayed on the UI are set through the environment variable. Connect to the database, disable registration and other information. As for other parameters, you can query them yourself

The Gitea port number maps two 3000 and 3001, namely HTTP access and SSH access. However, HTTP is basically used now, and SSH can be disabled with parameters

There is a dependency in the configuration file_ On attribute, which controls the deployment order. This means that Gitea deployment depends on postgres deployment

After successful installation with this configuration, two containers will be successfully deployed and a network will be created. After successful installation, Gitea can be accessed

Installation boot

The first visit will enter the installation wizard page, which is used to set the installation configuration information,

The wizard page will bring in the settings filled in the environment. Many information does not need to be set.

However, there is one administrator account setting,

This is for the administrator. If the administrator is not set, the first registered user will automatically become the administrator by default,

However, the administrator must be set when registration is prohibited

After setting up the administrator, click Install to install Gitea using the current page configuration. After installation, it will automatically jump to the home page

Push code

After success, you can create a warehouse and push previous web projects to this warehouse.

The IP address shown by HTTP in the PS diagram is the one set by the DOMAIN property during deployment.

Gateway Agent

configure gateway

The next step is to configure Gitea's gateway proxy.

First, configure the gateway. The gateway configuration will not be introduced

server {
    #The SSL access port number is 443
    listen 443 ssl http2;
    #Fill in the domain name of the binding certificate
    server_name gitea.mwjz.live;
    #journal
    error_log /var/log/nginx/gitea/error.log;
    access_log /var/log/nginx/gitea/access.log;
    #Certificate file
    ssl_certificate /etc/nginx/conf.d/ssl/gitea/gitea.mwjz.live_bundle.crt;
    #Certificate key file
    ssl_certificate_key /etc/nginx/conf.d/ssl/gitea/gitea.mwjz.live.key;

    ssl_ciphers SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!3DES:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://10.0.24.12:3000;
    }
}

After configuring the gateway, you also need to configure the configuration in Gitea service.

It will be troublesome to redeploy at this time, so you can modify Gitea's configuration file

The configuration file is / data / gitea / conf / APP ini. You only need to modify the root in this configuration file_ URL,DOMAIN, SSH_DOMAIN, and then restart the container

As an obsessive-compulsive disorder, I will also change the environment variables in the portal visualizer.

The portal visualizer supports modifying the configuration and then restarting the deployment

PS: note that it is not possible to directly change the environment variables in the portal visualizer. The configuration set by the environment variable has been written to the file.

Set upload size limit

Nginx has a size limit for uploading files. Uploading files larger than the limit size will return 403 error, and the default size limit is 1MB.

Therefore, by default, when the gateway (nginx) is used to push to Gitea, an error will be reported if the file is greater than 1MB.

To solve this problem, you only need to modify the client in the gateway (Nginx)_ max_ body_ The size attribute is OK.

server {
    #The SSL access port number is 443
    listen 443 ssl http2;
    #Fill in the domain name of the binding certificate
    server_name gitea.mwjz.live;
    #Upload size limit
    client_max_body_size 100M;
    #journal
    error_log /var/log/nginx/gitea/error.log;
    access_log /var/log/nginx/gitea/access.log;
    #Certificate file
    ssl_certificate /etc/nginx/conf.d/ssl/gitea/gitea.mwjz.live_bundle.crt;
    #Certificate key file
    ssl_certificate_key /etc/nginx/conf.d/ssl/gitea/gitea.mwjz.live.key;

    ssl_ciphers SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!3DES:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_pass http://10.0.24.12:3000;
    }
}

After modifying and restarting the gateway (Nginx), you can upload files larger than 1MB.