windows & linux操作系统实现 Gitea Actions

打印 上一主题 下一主题

主题 929|帖子 929|积分 2787

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
  1.   labels:
  2.     - "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
  • 本地编译环境需要提前配置好
编译打包工作流

以 软件编译打包 为例:
  1. name: Build & Pack Release  # 工作流的名称
  2. # 当有代码推送到仓库的 main 分支时,这个 Actions 工作流会被触发并执行
  3. on:
  4.   push:
  5.     branches:
  6.       - main
  7. jobs:
  8.   build:  # 定义一个名为 build 的作业
  9.     runs-on: windows  # 指定了作业运行的环境
  10.     steps:
  11.     - name: Run CMake build and install
  12.       run: |
  13.         Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser  # 允许本地脚本执行
  14.         echo "Set up conda environment"
  15.         cd D:/code/test/Build  # 构建输出的路径
  16.         conda init powershell
  17.         echo "Set up conda environment completed"
  18.         
  19.         echo "Starting software environment"
  20.         conda activate software
  21.         echo "Successfully started the software environment"
  22.         echo "Generate build files "
  23.         cmake -DCMAKE_INSTALL_PREFIX=D:/code/test/Build -S D:/code/test -B D:/code/test/Build
  24.         echo "Build file completed "
  25.         echo "Starting CMake build "
  26.         cmake --build D:/code/test/Build --config Release
  27.         echo "Completed CMake build"
  28.         echo "Starting CMake install"
  29.         cmake --install D:/code/test/Build --config Release
  30.         echo "Completed CMake install"
复制代码
编写完成后,提交并推送代码到远程堆栈
Linux 操作系统实现 gitea_actions

发起在Docker容器中运行Job,因此您需要首先安装Docker。 并确保Docker守护历程正在运行。
构建 docker 镜像

docker安装步骤
创建dockerfile

  1. # 使用官方 Ubuntu 镜像作为基础镜像
  2. FROM ubuntu:22.04
  3. # 安装必要的依赖
  4. RUN apt-get update && \
  5.     apt-get install -y --fix-missing \
  6.         curl \
  7.         git \
  8.         libunwind8 \
  9.         libicu-dev \
  10.         unzip \
  11.         sudo && \
  12.     apt-get clean && \
  13.     rm -rf /var/lib/apt/lists/*
  14. # 创建用户和工作目录
  15. RUN useradd -m runner && mkdir -p /actions-runner
  16. USER root
  17. WORKDIR /actions-runner
  18. # 下载和解压 GitHub Actions Runner
  19. 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 \
  20.     && tar xzf actions-runner-linux-x64-2.308.0.tar.gz
  21. # 启动脚本
  22. COPY start.sh /actions-runner/start.sh
  23. RUN chmod +x /actions-runner/start.sh
  24. 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 中
  1. # Example configuration file, it's safe to copy this as the default config file without any modification.
  2. log:
  3.   # The level of logging, can be trace, debug, info, warn, error, fatal
  4.   level: info
  5. runner:
  6.   # Where to store the registration result.
  7.   file: .runner
  8.   # Execute how many tasks concurrently at the same time.
  9.   capacity: 1
  10.   # Extra environment variables to run jobs.
  11.   envs:
  12.     A_TEST_ENV_NAME_1: a_test_env_value_1
  13.     A_TEST_ENV_NAME_2: a_test_env_value_2
  14.   # Extra environment variables to run jobs from a file.
  15.   # It will be ignored if it's empty or the file doesn't exist.
  16.   env_file: .env
  17.   # The timeout for a job to be finished.
  18.   # Please note that the Gitea instance also has a timeout (3h by default) for the job.
  19.   # So the job could be stopped by the Gitea instance if it's timeout is shorter than this.
  20.   timeout: 3h
  21.   # Whether skip verifying the TLS certificate of the Gitea instance.
  22.   insecure: false
  23.   # The timeout for fetching the job from the Gitea instance.
  24.   fetch_timeout: 5s
  25.   # The interval for fetching the job from the Gitea instance.
  26.   fetch_interval: 2s
  27. cache:
  28.   # Enable cache server to use actions/cache.
  29.   enabled: true
  30.   # The directory to store the cache data.
  31.   # If it's empty, the cache data will be stored in $HOME/.cache/actcache.
  32.   dir: ""
  33.   # The host of the cache server.
  34.   # It's not for the address to listen, but the address to connect from job containers.
  35.   # So 0.0.0.0 is a bad choice, leave it empty to detect automatically.
  36.   host: ""
  37.   # The port of the cache server.
  38.   # 0 means to use a random available port.
  39.   port: 0
  40. container:
  41.   # Specifies the network to which the container will connect.
  42.   # Could be host, bridge or the name of a custom network.
  43.   # If it's empty, act_runner will create a network automatically.
  44.   network: ""
  45.   # Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker).
  46.   privileged: false
  47.   # And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway).
  48.   options:
  49.   # The parent directory of a job's working directory.
  50.   # If it's empty, /workspace will be used.
  51.   workdir_parent:
复制代码
使用 docker 运行 runner


  • docker 运行
  1. docker run `  
  2.     -v $(pwd)/config.yaml:/config.yaml `  
  3.     -v $(pwd)/data:/data `  
  4.     -v /var/run/docker.sock:/var/run/docker.sock `  
  5.     -e CONFIG_FILE=/config.yaml `  
  6.     -e GITEA_INSTANCE_URL=<instance_url>`  
  7.     -e GITEA_RUNNER_REGISTRATION_TOKEN=<registration_token>`
  8.     --name gitea-runner `  
  9.     -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
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连密封材料

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表