前言

搭建本地 Gitea Action 测试环境,兼容 Github Action。Docker 的安装与使用方式请参阅官方文档。

部署流程

Gitea 与 Runner 都用容器部署。

环境

Ubuntu: Install Docker Engine on Ubuntu | Docker Docs

or

Windows: Install Docker Desktop on Windows | Docker Docs (with WSL)

本机 IP 地址为 172.30.138.45

Gitea

docker compose up -d 启动 gitea 和 postgres 容器

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
version: "3"

networks:
gitea:
external: false

services:
server:
image: gitea/gitea:1.21.4
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=postgres
- GITEA__database__HOST=db:5432
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
restart: always
networks:
- gitea
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
depends_on:
- db

db:
image: postgres:14
restart: always
environment:
- POSTGRES_USER=gitea
- POSTGRES_PASSWORD=gitea
- POSTGRES_DB=gitea
networks:
- gitea
volumes:
- ./postgres:/var/lib/postgresql/data

访问 http://172.30.138.45:3000/ 执行安装,默认配置不改动,注意在 Optional Settings / Administrator Account Settings 中设置管理员账户

打开折叠栏设置管理员账户,然后再点击 Install Gitea

Act Runner

通过 act_runner 镜像生成配置文件

1
docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml

默认配置如下

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Example configuration file, it's safe to copy this as the default config file without any modification.

# You don't have to copy this file to your instance,
# just run `./act_runner generate-config > config.yaml` to generate a config file.

log:
# The level of logging, can be trace, debug, info, warn, error, fatal
level: info

runner:
# Where to store the registration result.
file: .runner
# Execute how many tasks concurrently at the same time.
capacity: 1
# Extra environment variables to run jobs.
envs:
A_TEST_ENV_NAME_1: a_test_env_value_1
A_TEST_ENV_NAME_2: a_test_env_value_2
# Extra environment variables to run jobs from a file.
# It will be ignored if it's empty or the file doesn't exist.
env_file: .env
# The timeout for a job to be finished.
# Please note that the Gitea instance also has a timeout (3h by default) for the job.
# So the job could be stopped by the Gitea instance if it's timeout is shorter than this.
timeout: 3h
# Whether skip verifying the TLS certificate of the Gitea instance.
insecure: false
# The timeout for fetching the job from the Gitea instance.
fetch_timeout: 5s
# The interval for fetching the job from the Gitea instance.
fetch_interval: 2s
# The labels of a runner are used to determine which jobs the runner can run, and how to run them.
# Like: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"]
# If it's empty when registering, it will ask for inputting labels.
# If it's empty when execute `deamon`, will use labels in `.runner` file.
labels: []

cache:
# Enable cache server to use actions/cache.
enabled: true
# The directory to store the cache data.
# If it's empty, the cache data will be stored in $HOME/.cache/actcache.
dir: ""
# The host of the cache server.
# It's not for the address to listen, but the address to connect from job containers.
# So 0.0.0.0 is a bad choice, leave it empty to detect automatically.
host: ""
# The port of the cache server.
# 0 means to use a random available port.
port: 0
# The external cache server URL. Valid only when enable is true.
# If it's specified, act_runner will use this URL as the ACTIONS_CACHE_URL rather than start a server by itself.
# The URL should generally end with "/".
external_server: ""

container:
# Specifies the network to which the container will connect.
# Could be host, bridge or the name of a custom network.
# If it's empty, act_runner will create a network automatically.
network: ""
# Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker).
privileged: false
# And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway).
options:
# The parent directory of a job's working directory.
# If it's empty, /workspace will be used.
workdir_parent:
# Volumes (including bind mounts) can be mounted to containers. Glob syntax is supported, see https://github.com/gobwas/glob
# You can specify multiple volumes. If the sequence is empty, no volumes can be mounted.
# For example, if you only allow containers to mount the `data` volume and all the json files in `/src`, you should change the config to:
# valid_volumes:
# - data
# - /src/*.json
# If you want to allow any volume, please use the following configuration:
# valid_volumes:
# - '**'
valid_volumes: []
# overrides the docker client host with the specified one.
# If it's empty, act_runner will find an available docker host automatically.
# If it's "-", act_runner will find an available docker host automatically, but the docker host won't be mounted to the job containers and service containers.
# If it's not empty or "-", the specified docker host will be used. An error will be returned if it doesn't work.
docker_host: ""
# Pull docker image(s) even if already present
force_pull: false

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

登录管理员账号,进入 Site Administration / Actions /Runners(即 http://172.30.138.45:3000/admin/actions/runners)

点击 Create new Runner 拷贝 registration token

启动并注册 Runner

1
2
3
4
5
6
7
8
9
10
11
docker run \
-v $PWD/config.yaml:/config.yaml \
-v $PWD/data:/data \
-v /var/run/docker.sock:/var/run/docker.sock \
-e CONFIG_FILE=/config.yaml \
-e GITEA_INSTANCE_URL=http://172.30.138.45:3000/ \
-e GITEA_RUNNER_REGISTRATION_TOKEN=5YvrjeLGZYVuvrxKe8v00z1GVrixKATnM53MXXHF \
-e GITEA_RUNNER_NAME=gitea_runner \
--name gitea-runner \
-p 8088:8088\
-d gitea/act_runner:latest

刷新页面可以看到 Runner,注意到这里的 Type 是 Global,即该 Gitea 实例中的所有 Action 都可以使用该 Runner,另外,启动容器时没有指定 GITEA_RUNNER_LABELS 因此 Labels 使用了默认值 ubuntu-latestubuntu-22.04ubuntu-20.04ubuntu-18.04

测试 Gitea Action

创建仓库并在 Settings 中启用 Actions,点击 Update Settings 应用修改

直接在仓库中创建新文件 .gitea/workflows/demo.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]
jobs:
Explore-Gitea-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ gitea.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

查看 Actions 面板

Job 执行模式

Runner 的 Label 标记如何执行 Job,以默认的 ubuntu-latest:docker://node:16-bullseye 为例,当 runs-on 配置为 ubuntu-latest 时,Runner 会使用 node:16-bullseye 镜像创建容器来执行。

如果想要在主机上直接运行,则需要将 Label 设置为 ubuntu-22.04:host,意思是当 runs-on 配置为 ubuntu-22.04 时,Runner 会使用其宿主机的环境执行。注意,如果 Runner 是以容器方式启动的,那么这里的 host 就是容器,如果 Runner 直接运行在物理机上,那么这里的 host 就是物理机。

问题解决

报错 Cannot find: node in PATH

Runner 容器缺失 nodejs 环境

1
docker exec gitea-runner apk add --no-cache nodejs

使用 ssh 时要求输入密码

由于 Gitea 运行在容器中,直接使用仓库地址可能会出现要求 git@ip 的密码,通过 ssh 默认端口连接到的是主机而不是容器,默认配置文件配置的是 222 端口,可以用以下命令显式指定端口

1
git remote set-url gitea ssh://git@ip:222/user/repo.git

Runner 找不到目录 /root/.cache

创建 Runner 容器时映射 /root/.cache:/root/.cache

1
2
3
4
5
6
7
8
9
10
11
12
docker run \
-v $PWD/config.yaml:/config.yaml \
-v $PWD/data:/data \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /root/.cache:/root/.cache \
-e CONFIG_FILE=/config.yaml \
-e GITEA_INSTANCE_URL=http://172.30.138.45:3000/ \
-e GITEA_RUNNER_REGISTRATION_TOKEN=5YvrjeLGZYVuvrxKe8v00z1GVrixKATnM53MXXHF \
-e GITEA_RUNNER_NAME=gitea_runner \
--name gitea-runner \
-p 8088:8088\
-d gitea/act_runner:latest

Runner 执行 Job 超时

需要修改 Gitea 和 Runner 两处配置,默认值都是 3h,修改完毕后重启容器即可。

  1. Gitea ENDLESS_TASK_TIME

gitea/conf/app.ini

1
2
[actions]
ENDLESS_TASK_TIMEOUT = 24h
  1. Runner timeout

config.yaml

1
2
runner:
timeout: 24h

参阅