Gitea Actions 介绍
Gitea Actions 是指在 Gitea 上结合使用 GitHub Actions 或其他 CI/CD 工具的概念。具体来说,Gitea 是一个开源的自助托管 Git 服务软件,类似于 GitHub 或 GitLab,它允许你在自己的服务器上搭建和管理 Git 仓库。而 GitHub Actions 是 GitHub 提供的一种持续集成和持续部署(CI/CD)工具,它允许开发者根据事件触发自动化流程,例如在代码推送后自动运行测试、构建和部署到不同的环境。所以,在 Gitea 上使用 GitHub Actions ,通常是指利用 GitHub Actions 的能力来对托管在 Gitea 上的 Git 仓库进行自动化测试、构建和部署等操作。
Windows 操作系统实现 gitea_actions
配置runner
说明
- act runner 直接在本机上运行Job;
- 通过下载二进制文件安装 act runner;
- 配置 runner 之前需要先启用仓库中的 actions,开启完成后还需要获取注册令牌
启用actions
步骤:进入远程仓库 – 设置 – Actions – 启用 Actions – 更新仓库设置
获取注册令牌
步骤:进入远程仓库 – 设置 – Actions – Runners – 创建 runner – 复制 REGISTRATION TOKEN
配置 runner
-
下载 runner 二进制文件
runner下载地址 -
生成配置文件
下载的runner可能需要改名字,改成 act_runner.exe ;在 act_runner.exe 所在目录使用 powershell 运行
./act_runner generate-config > config.yaml
-
更改配置文件
修改步骤2生成的config.yaml文件,将标签改为windows:host
labels:
- "windows:host"
-
注册 runner (非交互方式注册 runner )
在 act_runner.exe 所在目录使用 powershell 运行
./act_runner register --no-interactive --instance <instance_url> --token <registration_token> --name <runner_name> --labels <runner_labels>
- instance_url: Gitea 实例的 URL,例如 https://gitea.com/ 或 http://192.168.8.8:3000/
- registration_token: 注册令牌
- runner_name: Runner名称(可选)。如果留空,将使用主机名
- runner_labels: Runner标签(可选)。如果留空,将使用默认标签(ubuntu-latest)
-
运行runner
启动 runner 运行,编译打包工作流。(在 act_runner.exe 所在目录使用 powershell 运行)
./act_runner daemon
编写actions工作流
说明
- 工作流语法查看路径
- 要使 Gitea 发现存储库中的任何 Gitea Actions 工作流程,您必须将工作流程文件保存在名为
..gitea/workflows
- 工作流文件扩展名
.yml
或者.yaml
- 本地编译环境需要提前配置好
编译打包工作流
以 软件编译打包 为例:
name: Build & Pack Release # 工作流的名称
# 当有代码推送到仓库的 main 分支时,这个 Actions 工作流会被触发并执行
on:
push:
branches:
- main
jobs:
build: # 定义一个名为 build 的作业
runs-on: windows # 指定了作业运行的环境
steps:
- name: Run CMake build and install
run: |
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # 允许本地脚本执行
echo "Set up conda environment"
cd D:/code/test/Build # 构建输出的路径
conda init powershell
echo "Set up conda environment completed"
echo "Starting software environment"
conda activate software
echo "Successfully started the software environment"
echo "Generate build files "
cmake -DCMAKE_INSTALL_PREFIX=D:/code/test/Build -S D:/code/test -B D:/code/test/Build
echo "Build file completed "
echo "Starting CMake build "
cmake --build D:/code/test/Build --config Release
echo "Completed CMake build"
echo "Starting CMake install"
cmake --install D:/code/test/Build --config Release
echo "Completed CMake install"
编写完成后,提交并推送代码到远程仓库
Linux 操作系统实现 gitea_actions
建议在Docker容器中运行Job,因此您需要首先安装Docker。 并确保Docker守护进程正在运行。
构建 docker 镜像
创建dockerfile
# 使用官方 Ubuntu 镜像作为基础镜像
FROM ubuntu:22.04
# 安装必要的依赖
RUN apt-get update && \
apt-get install -y --fix-missing \
curl \
git \
libunwind8 \
libicu-dev \
unzip \
sudo && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# 创建用户和工作目录
RUN useradd -m runner && mkdir -p /actions-runner
USER root
WORKDIR /actions-runner
# 下载和解压 GitHub Actions Runner
RUN curl -o actions-runner-linux-x64-2.308.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.308.0/actions-runner-linux-x64-2.308.0.tar.gz \
&& tar xzf actions-runner-linux-x64-2.308.0.tar.gz
# 启动脚本
COPY start.sh /actions-runner/start.sh
RUN chmod +x /actions-runner/start.sh
ENTRYPOINT ["/actions-runner/start.sh"]
构建 Docker 镜像
docker build -t my-custom-image .
-t
标志指定了镜像的名称(my-custom-image
)- 点号(
.
)表示当前目录是上下文目录
安装&注册 Runner
获取 runner 注册令牌
- 点击【首页右上角头像 — 管理后台 — Runners — 创建 Runner】 (与上文一致)
使用 docker 安装 runner
docker pull gitea/act_runner:nightly
- nightly标签使用最新的夜间构建版本
- latest标签是最新的稳定版本
准备挂载目录
mkdir -p /opt/gitea/gitea-act-runner/data # 挂载目录按自己需求自定
准备 Runner 配置文件
-
在挂载目录下准备配置文件
cd /opt/gitea/gitea-act-runner # 进入挂载目录
使用 CONFIG_FILE 环境变量指定配置文件,则可以忽略新建 config.yaml 这一步
touch config.yaml # 创建配置文件
-
编辑配置文件
使用 CONFIG_FILE 环境变量指定配置文件。确保将文件作为卷挂载到容器中:
docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml
上面的命令都是不完整的,因为现在还不是运行Act Runner的时候。 在运行Act Runner之前,需要先将其注册到Gitea实例中。
不使用 CONFIG_FILE 环境变量指定配置文件,也可以直接将下面示例代码复制到 config.yaml 中
# Example configuration file, it's safe to copy this as the default config file without any modification.
log:
# 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
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: ""
# The port of the cache server.
# 0 means to use a random available port.
port: 0
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:
使用 docker 运行 runner
- docker 运行
docker run `
-v $(pwd)/config.yaml:/config.yaml `
-v $(pwd)/data:/data `
-v /var/run/docker.sock:/var/run/docker.sock `
-e CONFIG_FILE=/config.yaml `
-e GITEA_INSTANCE_URL=<instance_url>`
-e GITEA_RUNNER_REGISTRATION_TOKEN=<registration_token>`
--name gitea-runner `
-d gitea/act_runner:nightly
- pwd:上文提到的挂载目录(/opt/gitea/gitea-act-runner)
- instance_url: Gitea 实例的 URL,例如 https://gitea.com/ 或 http://192.168.8.8:3000/
- registration_token: 注册令牌
- 检查运行状态
docker logs -f gitea-runner # 查看运行日志,控制台有打印 success 日志
编写 Actions 工作流
说明
- 工作流语法查看路径
- 要使 Gitea 发现存储库中的任何 Gitea Actions 工作流程,您必须将工作流程文件保存在名为
..gitea/workflows
- 工作流文件扩展名
.yml
或者.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@v3
- 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 }}."
编写完成后,提交并推送代码到远程仓库