Gitea 小记 自建 & Action

  • 自建 Gitea, 启用 Gitea Action 推送 Docker image

  • 资料来源:

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

  • 更新

    1
    2023.09.03 初始

导语

最近尝试将常用代码和容器做个完全备份, 321 这样, 没想到搭进去一个周末的时间..闲话打住..

这一篇是 自建 Gitea , 启用 Gitea Action 推送 docker 镜像到本地私有仓库 (其实还是 gitea), 一些步骤并不完全,以记录坑为主.

要注意: Action 虽然已经进入正式版,但还可能会有一些变化,请关注官方文档,下面会贴出具体的版本.

  • 硬件: 4125U 工控机,分到 Gitea 虚拟机的是 2C 2G;
  • Gitea: git(http ssh) 和 dockers 仓库 (http) 1.20
  • Action: act_runner:latest

自建 Gitea

参考 官方#使用 Docker 安装(rootless)

个人托管代码量不大,数据也不会很多,就沿用 sqlite3 数据库了,即使全量备份也没有多大.

rootless 模式要绝对确保,当前用户的 UID | GID 与 gitea 容器相同,默认是 1000:1000.

下面是我的配置文件,仅供参考.

  • 宿主机默认用户 UID | GID 已经是 1000:1000,因此未作更改.
  • 并没有使用数据卷,而是挂载到了本地目录.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: "2"

x-test: &default
restart: always
logging:
driver: json-file
options:
max-size: "5m"
max-file: "3"

services:
gitea:
<<: *default
image: gitea/gitea:1.20-rootless
restart: always
volumes:
- ./data:/var/lib/gitea
- ./config:/etc/gitea
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "80:3000" # http
- "2222:2222" # ssh

启用 Action

Gitea action 目标是兼容 GitHub action,提供相同的体验.但目前还有一些功能未支持,使用前请确认兼容性.

启用 Action 请参考 官方文档 真的写的非常仔细,下面是过程中的一些配置文件参考.

Action 独立于 gitea 需要单独启用,并在 gitea 注册.

这里是 docker-compose 启动

  • 3000 端口,与 gitea 声明在同一个文件.
  • 需要暴露宿主机的 docker
  • 变量声明在 .env 文件,方便管理.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
version: "2"

x-test: &default
restart: always
logging:
driver: json-file
options:
max-size: "5m"
max-file: "3"

services:
gitea:
xxx
runner:
<<: *default
image: gitea/act_runner:latest
depends_on:
- gitea
environment:
CONFIG_FILE: /config.yaml
GITEA_INSTANCE_URL: "${INSTANCE_URL}"
GITEA_RUNNER_REGISTRATION_TOKEN: "${REGISTRATION_TOKEN}"
volumes:
- ./action/config.yaml:/config.yaml
- ./action/data:/data
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 3000:3000

注册的 action 配置文件, 有一些坑,已中文注明,隐去了无用的注释.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
log:
# trace, debug, info, warn, error, fatal
level: info

runner:
# Where to store the registration result.
file: .runner
# 小机器并行 1 个就够了
capacity: 1
envs:
A_TEST_ENV_NAME_1: a_test_env_value_1
A_TEST_ENV_NAME_2: a_test_env_value_2
env_file: .env
timeout: 3h
insecure: false
fetch_timeout: 5s
fetch_interval: 2s
# 如果生成的配置文件这里为空,请将其填充如下内容
# action 声明 `runs-on:` 时对应下面的 ubuntu-latest 等
# 绝对不能为空 否则找不到对应 match 对应 https://docs.gitea.com/zh-cn/usage/actions/act-runner#%E6%A0%87%E7%AD%BE
labels: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"]

# 对应 https://docs.gitea.com/zh-cn/usage/actions/act-runner#%E6%A0%87%E7%AD%BE ,一般还是启用 cache 加快构建
cache:
# 启用
enabled: true
dir: ""
# 本地runner ip
host: "192.168.4.15"
# 本地runner 端口
port: 3000
external_server: ""

container:
# 要固定 runner 暴露的 ip 和端口因此是 host
network: "host"
# Docker-in-Docker 模式要求
privileged: true
options:
workdir_parent:
valid_volumes: []
docker_host: ""

host:
workdir_parent:

启用 软件仓库

gitea 提供了大量软件仓库的支持,而需要的是容器的支持.

gitea 提供的是符合 Open Container Initiative(OCI) 标准的托管

因为私有部署时候,http 的访问,给我写 action 时候带来了不小麻烦 😒😒😒😒…

同样过程参考官方文档,完全没有任何坑..非常顺利..

  • 启用了 2fa 的账户,需要使用 API 生成的 token 替换密码.
    • API 只启用软件包的读写权限即可.

Action 编译推送 Docker 容器

先说结论:

  • https 部署的 gitea 基本 github action 脚本通用,没啥问题.
  • http 部署的就很头疼了, 主要是 docker/login-action@v2 不支持登录 http 仓库太难受了..
    • 算是最后有解, 但时间是一个周末😒😒

因为编译 docker image 时经常遇到 docker in docker 的情况, 而参考 act-runner 文档 默认 ubuntu-latest 运行的环境是 node:16-bullseye,基于 debian 的环境,也没有 Docker.

为了方便,干脆将主要编译的 docker 容器也本地化.

  • 参考 Failed to run job about docker action#issuecomment-733658catthehacker/ubuntu:custom-22.04 上安装 docker 然后推到 action ,这样免得每次都需要安装 docker.

经过这个步骤,action 中的 runs-on 就变成了 docker-build

docker/login-action@v2 不支持 http 的问题, 在 issues/53#issuecomment-771142991 提供了一个示例,基本是 Set insecure registry 再登录,替换掉 docker/login-action@v2.

解决上面的问题后,再修改改获取 tag 方式. action 文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
name: Publish Docker image

on:
push:
# branches:
# - "master"
tags:
- "v*"

jobs:
docker:
runs-on: docker-build
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: "master"
- name: Set insecure registry
run: |
echo "{ \"insecure-registries\": [\"${{ secrets.DOCKER_REGISTRY }}\"] }" | tee /etc/docker/daemon.json
service docker restart
- name: Login to Docker registry
run: |
echo "${{ secrets.DOCKER_TOKEN }}" | docker login ${{ secrets.DOCKER_REGISTRY }} -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
- name: Get Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
name=${{ secrets.DOCKER_REGISTRY_TAG }}/${{ secrets.DOCKER_IMAGE_NAME }}
- name: Build and push
uses: docker/build-push-action@v4
env:
ACTIONS_RUNTIME_TOKEN: ""
with:
context: ./docker
file: ./docker/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

一些注意事项:

  • 提前在 secrets 中配置需要的参数
    • DOCKER_REGISTRY: gitea 地址,带 http 的 DOCKER_USERNAME DOCKER_TOKEN: 用户名和密码
  • DOCKER_REGISTRY_TAG: gitea 地址,不带 http,用于 Get Docker meta 这一步, tags 上别挂上 dockerhub 的网址..
    • 推送到私库,docker push 时候需要给镜像带上 私库的地址, 如果不特别指明推送目的地就成了 dockerhub 了…
    • 例如 alpine:latest, 推送时候应该是 192.168.1.1/test/alpine:latest 这样才行.
  • DOCKER_IMAGE_NAME 是 gitea 的要求必须是类似 test/alpine 而不能只是 alpine.
  • 更详细定义 tags 请参考 docker-metadata-action

结束语

自行托管的一套流程总算是运转起来了, 想起来那个著名梗图: 从刷路由器到开机房 😒 😂