有老板赞助了我一台服务器,总算是能做Gitea Action了
(阿里99一年的性能太差了,没有做的意义)
<0x01> Gitea的Action架构
Gitea中,Action并不是自带支持的
这很正常,毕竟Action要部署服务,自然是需要用到容器服务的
Gitea通过act runner这个程序实现了Action的支持
这样可以将Gitea服务器与Runner服务器分离
而且也方便多添加几个Runner
<0x02> 如何配置
这个act runner可以在本机运行,也可以通过Docker运行
为了确保最佳兼容性,这边选择通过Docker运行
首先是给服务器安装docker,这个每个系统都有自己的安装方式
需要注意的是,不要直接apt install docker,尤其是Debian
因为Debian库中的Docker版本很老了,很可能导致些意外情况
建议按照官方文档安装Docker
这里通过docker-compose的方式配置
mkdir gitea-actrunner
cd gitea-actrunner
mkdir data # 如果要配置action/cache的话
nano docker-compose.yaml
配置文件如下
version: "3.8"
services:
act_runner:
image: gitea/act_runner:latest
container_name: act_runner
restart: always
environment:
# 172.17.0.1是Docker容器访问宿主机的地址,3000是gitea默认端口号
- GITEA_INSTANCE_URL=http://172.17.0.1:3000
# 这里输入你的注册Token
- GITEA_RUNNER_REGISTRATION_TOKEN=Your Registration token
# 输入你希望给这个Runner的名字
- GITEA_RUNNER_NAME=Your runner name
# 这里是建议的可用Runner标签
- GITEA_RUNNER_LABELS=ubuntu-latest:docker://node:16-bullseye,ubuntu-22.04:docker://node:16-bullseye,ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster
- CONFIG_FILE=/config.yaml
volumes:
- ./config.yaml:/config.yaml
- ./data:/data
- /var/run/docker.sock:/var/run/docker.sock
然后docker compose up即可启动
如果没报错,并且gitea的注册界面出现了新的runner就是成功
<0x03> 测试Action
创建一个仓库,用来测试action
在仓库的.gitea/workflow/下创建一个.yaml文件
# demo.yaml
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 }}."
<0x04> 解决国内获取Action脚本慢的问题
众所周知,国内访问Github有点小问题
而上面的用到了actions/checkout@v4
这需要从Github上下载这个checkout脚本
有些时候,访问就很慢了
可以在自己的gitea中把需要的脚本都Clone过来
这有些费时费力,但确实是最稳妥的方法
如果是快速测试能否使用Action,也可以用下面的方法
打开config.yaml
修改下面的选项
# ...
runner:
# ...
github_mirror: 'https://gh-proxy.com/https://github.com'
# ...
# ...
这会指引act runner通过镜像clone脚本
然后重启runner即可
<0x05> 为什么用Docker运行
本来我是想本地运行act runner,但到这服务器的公网是NAT过来的,就行不通
为什么呢
首先NAT过来的公网是不能ping到自己的
也就是说,假设服务器的ip是x.x.x.x
那么ping x.x.x.x是没有结果的
这就导致在注册时,runner会找不到gitea
有人会说了,为什么不能用127.0.0.1呢
这个也不行,这会导致容器内拉取不到gitea仓库
因为runner是在本机上,可以通过127.0.0.1访问gitea
但容器的127.0.0.1访问的就不是gitea了,是容器自己
除非指定runner运行job也在本机上
但这样又无法隔离不同的环境
所以说,通过Docker运行act runner是兼容性最佳的方案