Gitea 环境搭建
GitLab 的页面加载实在太慢,慢到无法忍受的地步,因此尝试切换成 Gitea 这种轻量化的解决方案,目前用下来非常满意。

基于 ArchLinux,Gitea 1.20.3 版本,选择 SQLite 作为数据库。

Gitea 安装配置

Git with a cup of tea!
https://github.com/go-gitea/gitea

Installation from binary
https://docs.gitea.com/installation/install-from-binary

安装 Gitea

需要注意的是,相比官方文档,这是一个非常简化的安装步骤,并不是最佳实践。

❯ sudo cp gitea-1.20.3-linux-amd64 /usr/bin/gitea
❯ sudo chmod +x /usr/bin/gitea

❯ sudo setcap 'cap_net_bind_service+ep' /usr/bin/gitea

这里的 setcap 只是临时的,用于在 gitea 用户下有权限监听 80 端口,由于后面使用 systemd service 进行管理,且加上了 CAP_NET_BIND_SERVICE 权限,因此后续即使升级替换 gitea 可执行程序也不需要再设置权限。

配置 Gitea

添加 gitea 用户:

❯ sudo useradd --system -s /bin/bash -m gitea

切换到 gitea 用户并初始化 gitea:

❯ sudo su gitea
$ cd
$ mkdir gitea
$ GITEA_WORK_DIR=~/gitea gitea web -p 80

访问 80 端口,并根据向导进行配置,配置完毕之后退出进程。

运行 Gitea

接下来使用 systemd 服务进行管理:

❯ sudo vi /lib/systemd/system/gitea.service
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target

[Service]
LimitNOFILE=524288:524288
Type=notify
User=gitea
Group=gitea
WorkingDirectory=/home/gitea/gitea/
ExecStart=/usr/bin/gitea
Restart=always
RestartSec=10s
EnvironmentFile=/etc/default/gitea
Environment=USER=gitea HOME=/home/gitea GITEA_WORK_DIR=/home/gitea/gitea/
WatchdogSec=30s
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
PrivateUsers=false

[Install]
WantedBy=multi-user.target

❯ sudo systemctl daemon-reload
❯ sudo systemctl enable gitea
❯ sudo systemctl start gitea

Gitea Actions

Actions Quick Start
https://docs.gitea.com/usage/actions/quickstart

启用 actions 支持

当前 Gitea 默认不启用 actions 支持,因此首先需要启用 Gitea 的 actions 特性(注意先切换到 gitea 用户下进行操作):

$ cd ~/gitea/
$ vi custom/conf/app.ini
[actions]
ENABLED = true
DEFAULT_ACTIONS_URL = self

然后重启 gitea 服务。

DEFAULT_ACTIONS_URL: github: Default platform to get action plugins, github for https://github.com, self for the current Gitea instance.

Actions (actions)
https://docs.gitea.com/administration/config-cheat-sheet#actions-actions

安装 runner

下载 runner:

act runner
https://dl.gitea.com/act_runner/

❯ sudo cp act_runner-0.2.5-linux-amd64 /usr/bin/gitea-runner
❯ sudo chmod +x /usr/bin/gitea-runner

注册 runner

拿到 runner 注册所需的 token,如下图所示:

56a9558f0508dcc3bf6f0079c8f77535.png

为了避免权限问题,直接使用 root 用户(绝不可能是最佳实践)运行 runner,但仍然使用 gitea 的工作目录作为 runner 进程的工作目录:

# cd /home/gitea/gitea/
# gitea-runner register

特别注意 label 的定义(类似 Jenkins 定义 slave 的方法):

# cat .runner
{
  "name": "arch",
  "token": "607228da656b4ec7f3055452985d121c5e267f41",
  "address": "http://192.168.0.10:80",
  "labels": [
    "ubuntu-22.04:docker://ubuntu:22.04",
    "linux_amd64:host"
  ]
}

Labels
https://docs.gitea.com/usage/actions/act-runner#labels

Act runner label
https://docs.gitea.com/usage/actions/design#act-runner

运行 runner

使用 systemd 服务管理 runner:

❯ sudo vi /lib/systemd/system/gitea-runner.service
[Unit]
Description=Gitea Runner
After=syslog.target
After=network.target

[Service]
Type=exec
# WorkingDirectory is needed to lookup .runner
WorkingDirectory=/home/gitea/gitea/
# $HOME is needed for cfg.Host.WorkdirParent
ExecStart=/bin/bash -c 'exec env HOME=$$(getent passwd $$(id -u) | cut -d: -f6) /bin/bash -l -c "/usr/bin/gitea-runner daemon"'
Restart=always
RestartSec=30s

[Install]
WantedBy=multi-user.target

❯ sudo systemctl daemon-reload
❯ sudo systemctl enable gitea-runner
❯ sudo systemctl start gitea-runner

其中 bash -l 的目的是作为登录 shell 读取 ~/.bash_profile 配置(其实主要是设置 PATH 环境变量):

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option,
it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file,
it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and
executes commands from the first one that exists and is readable.

由于 systemd 的环境变量无法动态生成并配置(不可以 systemctl set-environment 这种全局方法),需要嵌套的两层 bash 进行规避,其中外层 bash 没有 HOME 环境变量,需要显式获取再传递给内层 bash,这样内层的 bash 既有了 HOME 环境变量又作为 login shell 能够读取到 .bash_profile 等配置文件(这些配置文件也很有可能引用了 HOME 环境变量),因此 runner 服务的 ExecStart 写的稍微有些扭曲。

注意 systemd 服务中设置 WorkingDirectoryHOME 是必须的,其中 WorkingDirectory 的设置显而易见,HOME 环境变量影响 cfg.Host.WorkdirParent 参数的默认值:

// act_runner/internal/pkg/config/config.go

func LoadDefault(file string) (*Config, error) {
    if cfg.Host.WorkdirParent == "" {
        home, _ := os.UserHomeDir()
        cfg.Host.WorkdirParent = filepath.Join(home, ".cache", "act")
    }
}

由于 systemd 服务默认不会设置 HOME 环境变量,导致 os.UserHomeDir() 得到的目录为空,从而导致 checkout 等 action 执行出错,因此 runner 服务文件对 HOME 环境变量的设置实际上是一箭双雕的事情。

当然,如果在 runner 的配置文件中进行显式配置,则无需这么复杂:

host:
  # The parent directory of a job's working directory.
  # If it's empty, $HOME/.cache/act/ will be used.
  workdir_parent:

当然,这只解决了 HOME 环境变量的问题,但是 runner 仍然没有继承 .bash_profile 等定义的各种环境变量。

GitHub action

Gitea action 兼容 GitHub action,因此可以直接进行镜像,当然也可以直接使用 GitHub 的 action:

❯ git clone --mirror git@github.com:pdm-project/setup-pdm.git
cd setup-pdm.git
❯ git remote set-url --push origin gitea@192.168.1.10:actions/setup-pdm.git
❯ git push

runner image

需要注意的是,GitHub action 实际上是在虚拟机里运行的,并不是我们通常理解的在容器里运行,

There is no official Docker image for that, because the GitHub runners are VMs.

Possible to pull github actions images?
https://github.com/orgs/community/discussions/26605

GitHub Actions runner images
https://github.com/actions/runner-images

2 Ways to Use Your Own Docker Image in Github Actions
https://yonatankra.com/2-ways-to-use-your-docker-image-in-github-actions/

Gitea CI/CD

各仓库的 Gitea Actions 需要独立配置,在仓库的 Advanced Settings 设置里开启:

3768bed8a68c0e4bb277acb0632e3d57.png

然后在仓库里创建 workflow 文件:

❯ mkdir .gitea/workflows
❯ vi .gitea/workflows/ci.yaml
name: checks on pr or push to master
on:
  push:
    branches:
      - 'master'
  pull_request:
    types: [opened, synchronize]
    branches:
      - 'master'

jobs:
  check:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - name: setup python
        uses: https://github.com/actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: setup pdm
        uses: http://192.168.0.10/actions/setup-pdm@v3
      - name: install dependencies
        run: pdm install
      - name: fmt
        run: pdm run fmt
      - name: lint
        run: pdm run lint

其中 ubuntu-22.04 label 的定义如下:

❯ cat .runner
{
  "labels": [
    "ubuntu-22.04:docker://ubuntu:22.04"
  ]
}

因此,这是一个 ubuntu:22.04 容器 runner,也就是说 workflow 里定义的操作将在 ubuntu:22.04 容器里运行,当然也可以直接使用 host 类型的 runner。

Use Actions
https://docs.gitea.com/usage/actions/quickstart#use-actions

Workflow syntax for GitHub Actions
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions

Where will the runner download scripts when using actions such as actions/checkout@v3?
https://docs.gitea.com/usage/actions/faq#where-will-the-runner-download-scripts-when-using-actions-such-as-actionscheckoutv3

使用 setup-python action 时可能出现如下的错误:

::group::Installed versions
Version 3.10 was not found in the local cache
::error::The version '3.10' with architecture 'x64' was not found for Arch rolling undefined.%0AThe list of all available versions can be found here: https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json

Version 3.10 with arch x64 not found
https://github.com/actions/setup-python/issues/401

这是因为 setup-python action 只支持 Ubuntu,因此需要注意 Gitea action 的运行环境,GitHub 提供的 action 运行环境是一个带了很多基础设施的虚拟机环境(最基础是 nodejs 环境),在使用 Gitea actions 时一定不要生搬硬套,如有需要最好构建一个大而全的容器镜像,或者直接注册合适的物理/虚拟主机作为 runner 环境。

user

创建普通用户,用户不能是已经存在的用户,否则会段错误(切换到 gitea 用户下进行操作,下同):

$ gitea admin user create -C ~/gitea/custom --username abc --password 123456 --email abc@example.com

创建管理员用户:

$ gitea admin user create -C ~/gitea/custom --admin --username abc --password 123456 --email abc@example.com

删除用户:

$ gitea admin user delete -C ~/gitea/custom --username abc

--custom-path path, -C path: Gitea’s custom folder path.

admin
https://docs.gitea.com/administration/command-line#admin

mirror / import

为了支持本地仓库的导入(如导入本地 gitlab 的仓库),需要在 custom/conf/app.ini 中添加如下选项:

[migrations]
ALLOW_LOCALNETWORKS = true

Migrations
https://docs.gitea.com/next/administration/config-cheat-sheet#migrations-migrations


最后修改于 2024-05-05