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

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

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

一、安装

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

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

1. gitea开启actions

修改gitea/conf/app.ini配置,若使用docker部署,可通过docker exec -it ${容器id} /bin/bash进入内部修改,修改完后重启 gitea 容器

1
2
3
# 添加此配置
[actions]
ENABLED = true

2. 查看Gitea Runner token

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

Setting-runner

3. 运行act runner

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

使用docker compose启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- act_data:/data
- act_cache:/root/.cache
volumes:
act_data:
act_cache:

4. 查看

在 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_runnerwindows_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

3. 使用 docker

在 Github Actions 中,默认工作环境可以直接使用 docker 命令,因此网上搜的 github actions 构建 docker 镜像等配置,放在 Gitea Actions 中运行不了,因为 gitea act_runner 默认运行镜像是 node:16-bullseye ,并没有 docker 环境,详见工单Gitea act_runner issue,最简单的解决办法是手动指定运行容器镜像

1
2
3
4
5
6
7
jobs:
My-Gitea-Actions:
runs-on: ubuntu-latest
# 此容器可使用docker,可查看 https://github.com/catthehacker/docker_images
container: catthehacker/ubuntu:act-latest
steps:
- run: docker version

4. 缓存工具目录

在步骤中安装构建工具时,例如 actions-setupactions-node等,它们都会去下载对应二进制文件,再解压到例如 /opt/hostedtoolcache 目录中,最后再配置环境变量,使得容器中能够使用相应的环境,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jobs:
My-Gitea-Actions:
runs-on: ubuntu-latest
# 此容器可使用docker,可查看 https://github.com/catthehacker/docker_images
container: catthehacker/ubuntu:act-latest
steps:
# 安装java环境
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'

- run: java -version

你会发现,每次执行工作流时,它都会重新下载二进制文件,并不会像 Github Actions 一样第一次下载,第二次因有缓存直接跳过,详情可查看工单cache tool folder,在 act_runner 修复此问题之前,我们可以指定环境变量 RUNNER_TOOL_CACHE 或借助 docker volume 来实现缓存功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
jobs:
My-Gitea-Actions:
runs-on: ubuntu-latest
container:
image: catthehacker/ubuntu:act-latest
# 方法二,手动指定持久化目录
volumes:
- ubuntu_hostedtoolcache:/opt/hostedtoolcache
env:
# 方法一,指定容器将工具缓存路径存放到 /toolcache ,该目录actRunner会默认持久化它
RUNNER_TOOL_CACHE: /toolcache
steps:
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'

- run: java -version

5. 支持多任务运行

5.1 准备config.yaml文件

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

5.2 修改配置

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

5.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.yaml
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- act_data:/data
- act_cache:/root/.cache
# add.2 将主机刚才的配置文件,挂载到容器内环境变量所指定文件路径
- /root/config.yaml:/config.yaml
volumes:
act_data:
act_cache:

6. 使用 actions/cache 超时

如果是通过docker部署的 act_runner ,因为容器隔离特性,其他运行的任务容器,无法访问到 act_runner 的cache相关服务,所以需要暴露出对应端口。

6.1 在config.yaml手动指定容器ip和端口

1
2
3
4
5
6
cache:
enabled: true
# 此为你主机ip,非容器ip
host: "192.168.100.101"
# 指定某个端口,不指定它会随机生成
port: 18088

6.2 暴露指定端口

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
- CONFIG_FILE=/config.yaml
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- act_data:/data
- act_cache:/root/.cache
- /root/config.yaml:/config.yaml
# 暴露端口,让其他容器能够访问得到
ports:
- 18088:18088
volumes:
act_data:
act_cache:

五、快速配置

5.1 准备 config.yaml 文件

其中 cache.host 改成你宿主机的 ip 地址

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
log:
level: warn
runner:
file: .runner
capacity: 4
envs:
RUNNER_TOOL_CACHE: /toolcache
env_file: .env
timeout: 1h
insecure: true
fetch_timeout: 10s
fetch_interval: 5s
labels:
[
"linux:docker://seepine/ubuntu:act",
"ubuntu:docker://seepine/ubuntu:act",
"ubuntu-latest:docker://seepine/ubuntu:act",
"ubuntu-22.04:docker://seepine/ubuntu:act",
]
cache:
enabled: true
dir: ""
# 改成你宿主机的ip
host: "192.168.100.110"
port: 18080
external_server: ""
container:
network: ""
privileged: false
options:
workdir_parent:
valid_volumes: []
docker_host: ""

host:
workdir_parent:

5.2 docker-compose运行runner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: "3"
services:
act_runner:
image: gitea/act_runner:0.2.6
environment:
- GITEA_INSTANCE_URL=https://example.com/
- GITEA_RUNNER_REGISTRATION_TOKEN=ulxFxA_这是gitea的runner中获取的加入token_Wsp7e2fH
- GITEA_RUNNER_NAME=docker_runner
- CONFIG_FILE=/config.yaml
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- act_data:/data
- act_cache:/root/.cache
# 此 /root/config.yaml 需指向上一步的文件路径
- /root/config.yaml:/config.yaml
ports:
- 18088:18088
volumes:
act_data:
act_cache:

5.3 运行即可

此时已能够获取到和github一致的runner环境,支持node、docker、yarn、pnpm等开发环境

六、案例

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

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

评论