Gitea Actions

Gitea 在 1.19.0 版本提供了该功能。从 1.21.0 版本开始该功能为默认开启,在大于等于 1.19.0 到小于 1.21.0 版本之间的版本,需要手动修改配置文件开启这项功能。

1
2
[actions]
ENABLED=true

配置文件开启该功能后,还需要在仓库设置中开启。参考

全部开启之后可以在项目中创建文件夹.gitea/workflows/ 在文件夹中创建任意名称以 yaml 结尾的文件。

比如这里创建 demo.yaml 并写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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@v4
- 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 }}."

推送相关修改到 Gitea 的仓库中,在仓库界面的 Actions 选项卡中查看任务执行的信息。

概念

Actions 是一个内置的CI/CD 系统框架。该功能需要依赖 runner 执行各种命令。每个 runner 可以指定不同的 label(标签)。

Gitea 提供的 Actions 是兼容 GitHub Actions 的功能。

Gitea 的 Runner 分为多种类型,通常情况下我们注册的都是全局 Runner,当然 Gitea 也支持你注册你自己的 Runner。也可以指定 Runner 归属于某个仓库或者组织。具体查询官方文档即可。

Runner

Runner 支持很多种方式安装,可以是物理机也可以是容器化部署。得益于使用 go 语言开发,所以各种硬件架构平台基本都支持。

本体也很简单就一个二进制文件,直接运行即可。

Runner 可以在官方仓库的 Release 页面下载,也可以在官方提供的下载页面下载。参考文档

本文采用物理机方式安装,并使用 systemd 管理。

部署(物理机)

首先下载好二进制文件,找个地方放好,并赋予可执行权限。

创建 systemd 服务配置文件,位置:/etc/systemd/system/act_runner.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Gitea Actions runner
Documentation=https://gitea.com/gitea/act_runner
After=docker.service

[Service]
EnvironmentFile=/root/env/gitea_runner/act_runner.service.env
ExecStart=/root/env/bin/act_runner daemon --config /root/env/gitea_runner/config.yaml
ExecReload=/bin/kill -s HUP $MAINPID
WorkingDirectory=/root/env/gitea_runner
TimeoutSec=0
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
  • 使用 EnvironmentFile 来指定服务运行时的环境变量。因为是物理机安装,部分指令在执行的时候需要读取一些信息。

  • 使用 WorkingDirectory 来指定服务运行时所处的目录位置。因为 Runner 默认会读取当前执行位置的一些必要信息。

  • ExecStart 为服务运行时具体执行的命令,这里跟上了配置文件参数。

这里给出相关配置。

环境变量配置文件:act_runner.service.env

1
PATH=/root/.nvm/versions/node/v20.17.0/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/env/bin

Runner 配置文件:config.yaml

默认配置文家可以使用命令导出一个默认配置:./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
90
91
92
93
94
95
96
97
98
99
100
101
# 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" or "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest"
# Find more images provided by Gitea at https://gitea.com/gitea/runner-images .
# If it's empty when registering, it will ask for inputting labels.
# If it's empty when execute `daemon`, will use labels in `.runner` file.
# ============================== 这里注释掉 =======================================
labels:
#- "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest"
#- "ubuntu-22.04:docker://gitea/runner-images:ubuntu-22.04"
#- "ubuntu-20.04:docker://gitea/runner-images:ubuntu-20.04"

# ============================== 启用缓存,指定缓存文件位置 ==========================
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: "/root/env/gitea_runner/actcache"
# 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.
# NOTE: There is no need to add the first '/' of the path as act_runner will add it automatically.
# If the path starts with '/', the '/' will be trimmed.
# For example, if the parent directory is /path/to/my/dir, workdir_parent should be path/to/my/dir
# 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: true
# Rebuild docker image(s) even if already present
force_rebuild: false

# ============================== 指定工作目录 ==========================
host:
# The parent directory of a job's working directory.
# If it's empty, $HOME/.cache/act/ will be used.
workdir_parent: /root/env/gitea_runner/act

注册

上方的部署步骤完成后,暂时还不能运行。因为我们的 Runner 并没有注册到我们自己的 Gitea 上。运行下面的命令注册

1
act_runner register --no-interactive --instance http://192.168.0.100:53021 --token q6QGc0huhfmMQxwvtW9lW8FgzerYFGnR6FOeUKr1 --labels "linux-221"

其中 token 需要登录 Gitea,在设置中创建 Runner 获取,如下图:

image-20240911104615039

labels 这里可以同时指定多个使用逗号间隔即可。

运行

上述步骤完成之后执行下面的命令启动服务并设置开机自动启动。

1
2
systemctl daemon-reload
systemctl enable act_runner --now

运行成功的话,这时候应该可以在 Gitea 的网页中看到该 Runner 的状态和信息。

使用

使用 Gitea 的 Actions 在前端某个分支推送或者合并 pr 时,触发执行。

编译项目并发布。

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
name: Build Project

on:
push:
branches:
- aaa # 监听 aaa 分支的 push 操作
pull_request:
branches:
- aaa # 监听 aaa 分支的 PR 合并操作

jobs:
build:
runs-on: linux-221 # 指定该任务运行在那个 runner 上,这里是 runner 的 label
steps:
- name: 分支切换
uses: https://gitea.com/actions/checkout@v4
with:
ref: aaa

- name: 获取版本号
id: version_step
run: |
VERSION=$(/root/env/bin/increment_version.sh project_name)
echo "VERSION=$VERSION" >> $GITHUB_ENV

- name: 安装环境依赖
run: yarn

- name: 前端编译
run: |
NODE_OPTIONS='--max-old-space-size=4096' make version=$VERSION build
env:
VERSION: ${{ env.VERSION }}

- name: 容器构建
run: |
make version=$VERSION docker-build
env:
VERSION: ${{ env.VERSION }}

- name: 运行容器(开发环境)
run: |
make version=$VERSION ssh-run
env:
VERSION: ${{ env.VERSION }}

手动执行

在最新版本 1.23.1 中已经支持该特性。根据官网更新日志给出的示例这里记录解释。

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
name: Docker Image CI

on:
workflow_dispatch:
inputs:
logLevel:
description: 'choice 类型参数测试'
required: true
default: 'warning'
type: choice
options:
- info
- warning
- debug
tags:
description: '测试标签设置'
required: false
type: boolean
boolean_default_true:
description: '测试 bool 默认值'
required: true
type: boolean
default: true
environment:
description: '环境变量'
type: environment
required: true
default: 'prod'
number_required_1:
description: '必须传递参数数字 '
type: number
required: true
default: 100
number_1:
description: '普通参数数字'
type: number
required: false

# 设置工作流级别的环境变量.
env:
inputs_logLevel: ${{ inputs.logLevel }}
inputs_tags: ${{ inputs.tags }}
inputs_boolean_default_true: ${{ inputs.boolean_default_true }}
inputs_environment: ${{ inputs.environment }}
inputs_number_1: ${{ inputs.number_1 }}
inputs_number_required_1: ${{ inputs.number_required_1 }}

jobs:
build:
runs-on: linux-221
steps:
- name: 分支切换
# uses: actions/checkout@v4 # 使用官方的 checkout action
uses: https://gitee.com/gldsly/checkout@v4
with:
ref: version_930_zichan # 指定要切换的分支

- name: 列出当前目录内容
run: ls -la

- name: 打印环境变量
run: env | grep inputs

- name: 打印 logLevel 输入参数
run: echo ${{ inputs.logLevel }}

- name: 打印 boolean_default_true 输入参数
run: echo ${{ inputs.boolean_default_true }}

- name: 根据 environment 输入参数设置环境变量
env:
PRODUCTION_API_URL: https://api.example.com
STAGING_API_URL: https://staging.example.com
# 注意本命令是把 echo 命令的输出写入到 $GITHUB_ENV 文件中, $GITHUB_ENV 是工作流环境变量
run: |
if [ "${{ inputs.environment }}" == "prod" ]; then
echo "API_URL=$PRODUCTION_API_URL" >> $GITHUB_ENV
else
echo "API_URL=$STAGING_API_URL" >> $GITHUB_ENV
fi
- name: 打印上一步设置的环境量
run: echo $API_URL

上述配置中 inputs 下的变量支持以下五种类型:

  • string :普通字符串
  • choice : 单选
  • boolean : 布尔值
  • number : 数字
  • environment :功能和字符串完全一致,但是声明该变量仅用于工作流。可以被字符串完全取代。

1.23.0 之前的版本

使用监控特定文件提交事件,来触发:

1
2
3
4
5
6
7
on:
# 采用监控特定文件变动方式触发该工作流执行
push:
branches:
- main # 监听 main 分支的 push 操作
paths:
- '.gitea/workflows/trigger/release'

上述配置表示监听 main 分支中文件 .gitea/workflows/trigger/release 的变动,该文件变动之后就开始执行工作流。

一些问题

全局 Runner 和个人 Runner

使用 Gitea root 账号 token 创建的 Runner 为全局。

个人账号 token 创建的 Runner 为个人。

可以在注册时手动指定 Runner 的归属(组织、仓库)。

配置文件中的 labels 注释问题

因为在注册命令时手动指定了。

Action 下载失败问题

默认 Gitea 从 GitHub 上下载对应的 action,你可以指定完全限定地址的 action。

或者给你的 Gitea 配置代理。在配置文件中追加代理配置。

1
2
3
4
[proxy]
PROXY_ENABLED = true
PROXY_URL = http://192.168.4.2:7890
PROXY_HOSTS = *.github.com

以上配置表示开启代理配置,代理服务器为 http://192.168.4.2:7890 当访问 *.github.com 时使用代理服务器。

详细代理配置,可以参考官方的文档进行配置。官方配置文件文档

还有另一种方式,就是使用自己仓库中的 actions,根据配置文档中的描述,你可以这么配置。

1
2
3
4
[actions]
ENABLED = true
DEFAULT_ACTIONS_URL = self
LOG_RETENTION_DAYS = 180

其中配置项 DEFAULT_ACTIONS_URL 改为 self 表示从自身查找。

Action 手动执行问题

截止 2024-09-24 官方版本目前 1.22.2 版本,并未支持该功能。根据 PR 中提供的信息,预计将会在 1.23 版本发布。参考地址: https://github.com/go-gitea/gitea/pull/28163

1.23 版本已经发布,参考该更新日志:https://blog.gitea.com/release-of-1.23.0/#major-highlights-actions

未来支持后 可以使用在 GitHub 仓库中的配置:

1
2
on:
workflow_dispatch: # 手动触发暂时