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
- 注册 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 镜像
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 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
编译打包工作流
此处拿官方例子进行演示,内容为:
[code]name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions |