Gitea Actions Runner 实现 DevOps 自动化部署 CI/CD
2025年2月12日约 495 字大约 2 分钟
Gitea 在 1.19 版本正式支持了 Gitea Actions,将 CI/CD 的功能内置到 Gitea 中,而且兼容 GitHub Actions 的语法,也就是说我们可以在 Gitea 使用类似 GitHub Actions 的功能了。
快速入手
首先目录结构如下:
# tree -L 1
.
├── cache
├── data
├── config.yaml
└── docker-compose.yml
执行命令 docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml
生成 config.yaml
配置文件 ,然后将其挂载到容器中。
Gitea 仓库的设置页面中,点击
Settings
-> Actions
-> Runners
-> Create new runner
,复制 Registration token
。
将 docker-compose.yml
文件中的环境变量 GITEA_RUNNER_REGISTRATION_TOKEN
需要修改为你的值。
执行 docker compose up -d
启动 Gitea 和 Runner。
# docker-compose.yml
services:
gitea:
image: gitea/gitea:1
container_name: gitea
restart: always
ports:
- 3000:3000
environment:
- TZ=Asia/Shanghai
- USER_UID=1000
- USER_GID=1000
volumes:
- ./data:/data
runner:
image: gitea/act_runner:latest
container_name: gitea-runner
restart: always
depends_on:
- gitea
environment:
- CONFIG_FILE=/config.yaml
- GITEA_INSTANCE_URL=http://gitea:3000
- GITEA_RUNNER_REGISTRATION_TOKEN=需要修改
- GITEA_RUNNER_NAME=gitea-runner
volumes:
- ./config.yaml:/config.yaml
- ./data:/data
- ./cache:/root
- /var/run/docker.sock:/var/run/docker.sock
工作流配置
actions.yml 工作流的语法可以参考:
- https://docs.github.com/zh/actions/writing-workflows/workflow-syntax-for-github-actions
- https://docs.gitea.com/usage/actions/comparison
本站点的 CI/CD 配置如下:
提示
默认配置文件下的标签 labels 如下:
# config.yaml
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"
runs-on
选项会匹配 labels
中的镜像,例如 runs-on: ubuntu-latest
,将会使用 node:16-bullseye
这个镜像。
# .gitea/workflows/actions.yml
jobs:
build:
runs-on: ubuntu-latest
action 脚本可以使用国内镜像加速,例如:https://gitee.com/actions-mirror/checkout@v4
。
# .gitea/workflows/actions.yml
name: Gitea Actions Build
on:
push:
paths-ignore:
- .github/**
- .commitlintrc.cjs
- .drone.yml
- .editorconfig
- .gitignore
- .markdownlint.json
- .prettierignore
- .prettierrc.cjs
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: https://gitee.com/actions-mirror/checkout@v4
with:
fetch-depth: 0
# 如果你文档需要 Git 子模块,取消注释下一行
# submodules: true
- name: 安装 pnpm
uses: https://gitee.com/mys1024/actions-setup-pnpm@v4
with:
version: 9
- name: 设置 Node.js
uses: https://gitee.com/actions-mirror/setup-node@v4
with:
node-version: 20
cache: pnpm
registry-url: https://registry.npmmirror.com
- name: Install dependencies
run: pnpm install
- name: Build artifact
id: build
env:
NODE_OPTIONS: --max_old_space_size=2048
run: |-
pnpm run build
- name: Upload artifact
uses: https://gitee.com/actions-mirror/upload-artifact@v3
with:
path: dist