玩转Gitea之Gitea Actions安装使用

warning: 这篇文章距离上次修改已过220天,其中的内容可能已经有所变动。
Gitea Actions是源于Github Actions的一个项目,是基于Github Actions开源项目改造而来,同时也兼容了Github Actions绝大多数的功能。大多数情况下可以无缝切换的使用。

Act-Runner

安装

这里同样是使用Docker Compose进行安装,安装之前先在部署好的Gitea上去获取Token用于注册Runner

  1. 通过头像下拉菜单管理后台ActionsRunners的路径可以找到系统级Runners列表,在这里注册的Runners可以被任何仓库、组织使用。

图1图1

  1. 点击创建Runner,复制显示的Token
    图2图2
  2. 修改Compose文件, 运行即可。

在运行前需要先生成一下配置文件:

# 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

启动后,在GiteaRunners列表中就可以看到刚刚注册进行的Runner了。

图3图3

配置

Act-Runner的配置其实已经很一键式,唯独有一个需要特别注意的地方Labels,这个地方需要额外关注一下。

Labels

label其实在传统的CI中也存在,但是存在感较弱。
Act-Runner Labels

labelActions中是直接用于匹配启动构建的Runner用的,在脚本中指定的runs-on就是指定的label,而在拥有这个lableRunner就会拉取到这个构建任务并运行起来。

用一张图来说明一下

示意图示意图

label写法的含义:

label例子运行环境说明
labelNameubuntuact-runner所处环境通常来说是直接在宿主机上跑./act-runner的方式,比如说windowsmacos等环境
labelName:hostwindows-10:host同上同上,这两者是等价的,host相当于缺省默认值
labelName:docker://imageNameubuntu-latest:docker://node:16-bullseye使用对应镜像,启动一个Docker容器来运行构建任务用于Docker环境部署的情况, 会比较方便,跑完构建任务后就会自动删除

不管runner配置中的labels如何写,runs-on匹配的始终是LabelName,后面的后缀/附加信息是不会被纳入匹配规则中的,只是用于Runner决定如何启动构建任务而已。

下列labelruns-on: ubuntu-latest的写法下是等价的:

  1. ubuntu-latest
  2. ubuntu-latest:host
  3. ubuntu-latest:docker://node:16-bullseye
  4. ubuntu-latest:docker://golang:1.21

注意事项

Docker构建操作失败

**原因:Docker部署的Runner,在pipeline中使用Docker CLI会报错,是因为默认的镜像中没有Docker CLI

相关ISSUE

解决方案:

  1. 使用papodaca/install-docker-action@mainaction进行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

评论已关闭