抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Gitea Actions 基本和 Github Actions 一模一样包括语法,所以就不再阐述它是个什么东西了,以下引用 Github Actions 的相关介绍

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行任务、测试和部署。用户可以创建工作流程来构建和测试存储库的每个拉取请求,或将合并的拉取请求部署到生产环境。
GitHub Actions 不仅仅是 DevOps,还允许用户在存储库中发生其他事件时运行工作流程。 例如,可以运行工作流程,以便在有人创建新问题时自动添加相应的标签。
GitHub 提供 Linux、Windows 和 macOS 虚拟机来运行工作流程,或者在自有的数据中心或云基础架构中托管运行器。

一、安装

本文默认已安装 Gitea 1.27.0 及以上版本,未安装请参考Gitea 搭建

安装前需要机器安装好 docker ,若未安装请参考Docker 快速入门。

1. 查看Gitea Runner token

登录管理员账号,在右上角头像选择后台管理、runner 页签中查看,若只是作为自己仓库的 runner,则只需在相应仓库的 设置-runner 中查看 token

Setting-runner

2. 运行act runner

注意:若不想跟随教程一步步走,可直接跳转到 五、快速配置 章节,直接搭建完全体

准备配置文件 docker-compose.yml ,使用docker compose up -d 命令启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: "3"
services:
act_runner:
# 注意,此处是 dind 模式,更加简单和安全
image: gitea/runner:4.1-dind
restart: unless-stopped
privileged: true
environment:
- GITEA_INSTANCE_URL=https://example.com/
- GITEA_RUNNER_REGISTRATION_TOKEN=ulxFxANKnyjsa这是上一步查看的tokeniSwGWsp7e2fH
- GITEA_RUNNER_NAME=docker_runner
volumes:
- act_data:/data
- act_cache:/root/.cache
volumes:
act_data:
act_cache:

3. 查看

在 runner 管理面板即可看到加入的runner,且状态为 Idle
runner_management

二、测试

1. 创建仓库

创建名为 actions-demo 的仓库,勾选初始化添加README.md,并在设置中开启 Actions ,即可看到多了 Actions 一栏
open_actions_1
open_actions_2
open_actions_3

2. 添加工作流文件

以下是一个示例,将它保存到 .gitea/workflows/build.yaml 时会触发 CI 工作,yaml 语法可参考 Github Actions Docs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: Gitea Actions Demo
run-name: ${{ github.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 ${{ github.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 ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo " The ${{ github.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 ${{ github.workspace }}
- run: echo " This job's status is ${{ job.status }}."

3. 查看执行结果

runner_test

三、变量运用

1. 默认上下文变量

在编写步骤文件时,可以直接使用默认的变量来实现想要的功能,语法为 ${{ xxx }},具体有哪些变量可查看Github Actions Context Docs

1
2
- run: echo ${{ github.ref }}
- run: echo ${{ github.repository }}

输出

1
2
refs/heads/main
seepine/actions-demo

2. 环境变量

环境变量分为默认环境变量和自定义环境变量,语法为 ${{ env.xxx }},具体请查看Github Actions Variables Docs

1
2
3
4
5
6
7
8
9
10
11
12
13
jobs:
My-Gitea-Actions:
runs-on: ubuntu-latest
# 自定义方式一
env:
CUSTOM_KEY: custom env value
steps:
# 自定义方式二
- run: echo CUSTOM_TOKEN=asdf1234 >> $GITHUB_ENV

- run: echo ${{ env.GITHUB_ACTION_REPOSITORY }}
- run: echo ${{ env.CUSTOM_KEY }}
- run: echo ${{ env.CUSTOM_TOKEN }}

输出

1
2
3
seepine/actions-demo
custom env value
asdf1234

3. Secrets变量

一般用于定义密码等敏感变量,此变量输出时会变成*,但不影响使用,在设置-Secrets中添加Key-Value即可

1
- run: echo ${{ secrets.CUSTOM_KEY }}

输出

1
***

4. output

许多时候我们会需要输出一些特定内容供他人获取,若输出到环境变量,我们很难随心定义key,因为有可能会与其他步骤的环境变量冲突而覆盖它,因此出现了output这个用法,最常见的即 Docker metadata

1
2
3
4
5
6
7
8
9
jobs:
My-Gitea-Actions:
runs-on: ubuntu-latest
steps:
- name: Gen Meta
id: my_meta # 指定一个id
run: echo CUSTOM_TOKEN=asdf1234 >> $GITHUB_OUTPUT

- run: echo ${{ steps.my_meta.outputs.CUSTOM_TOKEN }}

输出

1
asdf1234

四、进阶用法

1. 指定工作流运行 runner

若有多个runner节点,我们想指定某个工作流程运行在特定runner上,可在不同runner指定不同label用于区分(可在Runner管理面板,编辑其 labels),例如分别有两个 runner 是 linux 环境和 windows 环境,因此分别设置label为 linux_runner和 windows_runner

1
2
3
4
jobs:
My-Gitea-Actions:
runs-on: linux_runner
runs-on: windows_runner

2. 使用github的步骤脚本

在编写步骤配置时,通常都会引用别人写好的脚本,例如

1
2
3
4
5
- name: Login to DockerHub
uses: docker/login-action@v2

- name: Login to DockerHub
uses: my_custom/other-action@v2

此时 Gitea Actions 不一定能正常工作,因为它在

  • < 1.20 默认是访问 Gitea.com这个代码托管仓库,因此若脚本是在 Github 上时,它将无法下载脚本内容
  • >=1.20 默认访问 Github.com

所以当出现下载有问题时,我们可以完整写明脚本地址,例如

1
2
- name: Login to DockerHub
uses: https://github.com/my_custom/other-action@v2

也可以通过修改gitea的app.ini配置,改为从相应的仓库下载

1
2
3
4
[actions]
# 1.19 可直接填写任意url如:https://github.com
# 1.20起,不填默认从 github,填self表示从自建仓库下载
DEFAULT_ACTIONS_URL = self

4. 支持多任务运行

4.1 准备config.yml文件

通过 docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yml 生成

4.2 修改配置

1
2
3
runner:
# 修改此数字,4表示同时支持4个任务并行,数量最好根据你机器性能和所跑任务负载统一决定,并不是越高越好
capacity: 4

4.3 修改启动配置

修改 docker compose 配置后,重启生效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
version: "3"
services:
act_runner:
image: gitea/act_runner:latest
environment:
- GITEA_INSTANCE_URL=https://example.com/
- GITEA_RUNNER_REGISTRATION_TOKEN=ulxFxANKnyjsa这是上一步查看的tokeniSwGWsp7e2fH
- GITEA_RUNNER_NAME=docker_runner
# add.1 容器内指定配置文件路径
- CONFIG_FILE=/config.yml
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- act_data:/data
- act_cache:/root/.cache
# add.2 将主机刚才的配置文件,挂载到容器内环境变量所指定文件路径
- /root/config.yml:/config.yml
volumes:
act_data:
act_cache:

五、快速配置

5.1 准备 config.yml 文件

若需要代理,可放开第9行起 envs 中的 HTTP_PROXY 等环境变量,替换为你实际代理地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
log:
level: info

runner:
file: .runner
capacity: 2 # 并发
env_file: .env
timeout: 1h
insecure: true
fetch_timeout: 10s
fetch_interval: 5s
labels:
- "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest"
- "ubuntu-24.04:docker://gitea/runner-images:ubuntu-24.04"
- "ubuntu-22.04:docker://gitea/runner-images:ubuntu-22.04"
cache:
enabled: true
container:
# runner v4 需要手动挂载
options: --volume /host/toolcache:/opt/hostedtoolcache
valid_volumes:
- /host/toolcache

5.2 准备 daemon.json

配置 runner 内部 docker 配置,其中 registry-mirrors 国内源按需配置,若失效自行改成其他的

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "5",
"compress": "true"
},
"dns": ["172.16.99.100"],
"registry-mirrors": [
"https://docker.xuanyuan.me",
"https://docker.m.daocloud.io"
]
}

5.3 docker-compose 运行 runner

准备 docker-compose.yml 文件,与 config.yml 同目录

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
services:
gitea_runner:
image: gitea/runner:4.1-dind
restart: always #unless-stopped
privileged: true
environment:
- GITEA_INSTANCE_URL=https://example.com/
- GITEA_RUNNER_REGISTRATION_TOKEN=__gitea的runner中获取的加入token__
- GITEA_RUNNER_NAME=docker_runner
- CONFIG_FILE=/config.yml
# 需要代理自行修改
#- HTTP_PROXY=http://192.168.5.5:7890/
#- HTTPS_PROXY=http://192.168.5.5:7890/
#- NO_PROXY=127.0.0.1,localhost,172.17.0.1,172.16.99.99,192.168.5.5
volumes:
- ./act_data:/data
- ./act_cache:/root/.cache
- ./toolcache:/host/toolcache
- ./config.yml:/config.yml
- ./daemon.json:/etc/docker/daemon.json
dns:
- 172.16.99.100
networks:
net_runner:
ipv4_address: 172.16.99.99
deploy:
resources:
limits:
# 限制资源,按需删减
memory: 8G
cpus: 8

# 自建 dns, 用于 gitea 内网的情况,若不需要可删除,并把 gitea_runner 的 dns 和 5.2 节中 daemon.json 的 dns 去除
gitea_dnsmasq:
image: 4km3/dnsmasq:2.90-r3
container_name: ci-dns
restart: always
command:
# 把 192.168.5.5 改成你 gitea 内网 ip,若不在同一个内网,可根据指引,删除 gitea_dnsmasq
- --address=/git.example.com/192.168.5.5
- --server=223.5.5.5
networks:
net_runner:
# 指定 dns 服务的 ip,提供给 gitea_runner 和 其内部的 docker
ipv4_address: 172.16.99.100

networks:
net_runner:
ipam:
config:
- subnet: 172.16.99.0/24
gateway: 172.16.99.1

5.3 运行

执行命令启动即可

1
docker compose up -d

此时已能够获取到和 github 一致的 runner 环境,几乎无缝兼容 github 所有 actions,

六、案例

将以构建后端 SpringBoot with gradle 和前端 Vue3 with vite 两个项目为例,演示借助 Actions 构建前后端项目,并缓存构建过程中的工具,最后打包成 Docker 镜像并推送到镜像仓库

构建 SpringBoot Docker镜像
构建 Quarkus native Docker镜像
构建 Vue Docker 镜像
构建 Go Docker 镜像

评论