玩转Gitea之Gitea Actions安装使用
warning:
这篇文章距离上次修改已过220天,其中的内容可能已经有所变动。
Gitea Actions
是源于Github Actions
的一个项目,是基于Github Actions
开源项目改造而来,同时也兼容了Github Actions
绝大多数的功能。大多数情况下可以无缝切换的使用。
安装
这里同样是使用Docker Compose
进行安装,安装之前先在部署好的Gitea
上去获取Token
用于注册Runner
。
- 通过头像下拉菜单、管理后台、Actions、Runners的路径可以找到系统级的
Runners
列表,在这里注册的Runners
可以被任何仓库、组织使用。
在运行前需要先生成一下配置文件:
# Docker
docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml
这里我复制了一份出来:
需要配置一下cache.host
,用于保存缓存用的,端口也需要映射出来config.yaml
:
# 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: "填入宿主机IP"
# The port of the cache server.
# 0 means to use a random available port.
port: 9010
# 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
# 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:
gitea-runner.yml
:
version: "3.8"
services:
runner:
image: gitea/act_runner
restart: always
privileged: true
environment:
CONFIG_FILE: /config.yaml
GITEA_INSTANCE_URL: "gitea访问地址"
GITEA_RUNNER_REGISTRATION_TOKEN: "第二步的Token"
GITEA_RUNNER_NAME: "自定义Runner的名称用于显示"
GITEA_RUNNER_LABELS: "暂时不建议修改"
DOCKER_HOST: unix:///var/run/docker.sock # 这是docker的套接字,用于Docker in Docker
ports:
- 9010:9010 # 这里是映射缓存端口
volumes:
- 配置文件:/config.yaml
- 数据文件:/data
- /var/run/docker.sock:/var/run/docker.sock
启动后,在Gitea
的Runners
列表中就可以看到刚刚注册进行的Runner
了。
配置
Act-Runner
的配置其实已经很一键式,唯独有一个需要特别注意的地方Labels,这个地方需要额外关注一下。
Labels
label
其实在传统的CI
中也存在,但是存在感较弱。
Act-Runner Labels
label
在Actions
中是直接用于匹配启动构建的Runner
用的,在脚本中指定的runs-on
就是指定的label
,而在拥有这个lable
的Runner
就会拉取到这个构建任务并运行起来。
用一张图来说明一下
各label
写法的含义:
label | 例子 | 运行环境 | 说明 |
---|---|---|---|
labelName | ubuntu | act-runner 所处环境 | 通常来说是直接在宿主机上跑./act-runner 的方式,比如说windows ,macos 等环境 |
labelName:host | windows-10:host | 同上 | 同上,这两者是等价的,host 相当于缺省默认值 |
labelName:docker://imageName | ubuntu-latest:docker://node:16-bullseye | 使用对应镜像,启动一个Docker 容器来运行构建任务 | 用于Docker 环境部署的情况, 会比较方便,跑完构建任务后就会自动删除 |
不管runner
配置中的labels
如何写,runs-on
匹配的始终是LabelName
,后面的后缀/附加信息是不会被纳入匹配规则中的,只是用于Runner
决定如何启动构建任务而已。
下列label
在runs-on: ubuntu-latest
的写法下是等价的:
ubuntu-latest
ubuntu-latest:host
ubuntu-latest:docker://node:16-bullseye
ubuntu-latest:docker://golang:1.21
注意事项
Docker
构建操作失败
**原因:Docker
部署的Runner
,在pipeline
中使用Docker CLI
会报错,是因为默认的镜像中没有Docker CLI
解决方案:
- 使用
papodaca/install-docker-action@main
的action
进行cli
的安装。
示例脚本:
on:
push:
tags:
- "v*.*.*"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: 1.21
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Docker
uses: papodaca/install-docker-action@main
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
评论已关闭