Gitea 安装 Actions

起因是听说gitea自带action了, 兼容github action

另一个原因是 drone 在arm64位设备上似乎运行不了.

使用结论: 非常香, 和gitea融合更好, 更接近 github action体验

使用 gitea action

Act Runner | Gitea Documentation

Gitea Actions 搭建 - Seepine’s Blog

安装

gitea开启功能

  • gitea开启 功能, 路径data/gitea/conf/app.ini
1
2
[actions]  
ENABLED=true

重启

获取令牌

  • 获取令牌
    • 实例级别:管理员设置页面,例如 <your_gitea.com>/admin/actions/runners
    • 组织级别:组织设置页面,例如 <your_gitea.com>/<org>/settings/actions/runners
    • 存储库级别:存储库设置页面,例如 <your_gitea.com>/<owner>/<repo>/settings/actions/runners

安装 act_runner

  • 安装act_runner
1
2
3
4
5
# 下载
docker pull gitea/act_runner:latest

# 生成配置文件
docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
services:
  runner:
    image: gitea/act_runner:latest
    container_name: runner
    environment:
      - CONFIG_FILE=/config.yaml
      - GITEA_INSTANCE_URL=http://172.17.0.1:3000/
      - GITEA_RUNNER_REGISTRATION_TOKEN=令牌
      - GITEA_RUNNER_NAME=docker_runner
    restart: unless-stopped
    volumes:
      - ./config.yaml:/config.yaml
      - ./data:/data
      - ./cache:/root/.cache
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - 8088:8088

标签

Runner的标签用于确定Runner可以运行哪些Job以及如何运行它们。

默认标签为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

举例: ubuntu-latest:docker://node:16-bullseye表示可以运行runs-on: ubuntu-22.04的job, 并且该Job将在使用node:16-bullseye镜像的Docker容器中运行。

最新的镜像为: https://hub.docker.com/r/gitea/runner-images, 标签对应关系 buster: debian10, bullseye: debian11, -slim: 未安装python,git似乎也没有

如果要添加, 需要修改上面生成的config.yaml

快速使用

  • 储库的设置页面, 启用action, 更新仓库设置
  • 新建.gitea/workflows/文件夹, 新增demo.yaml

这是官方给的案例, 实际需要修改

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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@v4
      - 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 }}."
  • 需要设置代理env, 不然 action 下载不了
  • 默认的url前缀是 github.com , repository github-server-url需要改成gitea的
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]

jobs:
  Explore-Gitea-Actions:
    runs-on: ubuntu-latest
    env:
      http_proxy: http://172.17.0.1:7890
      https_proxy: http://172.17.0.1:7890
    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@v4
        with:
          repository: ${{ gitea.repository }}
          github-server-url: 'http://172.17.0.1:3000'
      - 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 }}."

使用代理

设置 env

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
name: Example Workflow

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    env:
      http_proxy: http://your-proxy-server:port
      https_proxy: http://your-proxy-server:port

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Run a multi-line script
      run: echo "This is a multi-line script"

设置cache

修改配置文件

1
2
3
4
5
6
7
cache:
  enabled: true
  dir: ""
  # 使用步骤 1. 获取的 LAN IP
  host: "172.17.0.1"
  # 使用步骤 2. 使用空闲的端口
  port: 8088

这里的设置了8088, 所以前面端口映射就是8088

默认上下文

在编写步骤文件时,可以直接使用默认的变量来实现想要的功能,语法为 ${{ xxx }},具体有哪些变量可查看Github Actions Context Docs

1
2
- run: echo ${{ github.ref }}
- run: echo ${{ github.repository }}
1
2
refs/heads/main
seepine/actions-demo

环境变量

环境变量分为默认环境变量和自定义环境变量,语法为 ${{ env.xxx }},具体请查看Github Actions Variables Docs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
jobs:
  My-Gitea-Actions:
    runs-on: ubuntu-latest
    # 自定义方式一
    env:
      CUSTOM_KEY: custom env value
    steps:
      # 自定义方式二
      - run: echo CUSTOM_TOKEN=asdf1234 >> $GITHUB_ENV

      - run: echo ${{ env.GITHUB_ACTION_REPOSITORY }}
      - run: echo ${{ env.CUSTOM_KEY }}
      - run: echo ${{ env.CUSTOM_TOKEN }}
1
2
3
seepine/actions-demo
custom env value
asdf1234

Secrets变量

一般用于定义密码等敏感变量,此变量输出时会变成*,但不影响使用,在设置-Secrets中添加Key-Value即可

1
- run: echo ${{ secrets.CUSTOM_KEY }}

output

许多时候我们会需要输出一些特定内容供他人获取,若输出到环境变量,我们很难随心定义key,因为有可能会与其他步骤的环境变量冲突而覆盖它,因此出现了output这个用法,最常见的即 Docker metadata

1
2
3
4
5
6
7
8
9
jobs:
  My-Gitea-Actions:
    runs-on: ubuntu-latest
    steps:
      - name: Gen Meta
        id: my_meta # 指定一个id
        run: echo CUSTOM_TOKEN=asdf1234 >> $GITHUB_OUTPUT

      - run: echo ${{ steps.my_meta.outputs.CUSTOM_TOKEN }}

指定runner

1
2
3
4
jobs:
  My-Gitea-Actions:
    runs-on: linux_runner
    runs-on: windows_runner

指定action的地址

默认是github仓库, 但可能连不上

1
uses: https://github.com/my_custom/other-action@v2

也可以通过修改gitea的app.ini配置,改为从相应的仓库下载

1
2
3
4
[actions]
# 1.19 可直接填写任意url如:https://github.com
# 1.20起,不填默认从 github,填self表示从自建仓库下载
DEFAULT_ACTIONS_URL = self

指定运行容器的镜像

gitea act_runner 默认运行镜像是 node:16-bullseye ,并没有 docker 环境

1
2
3
4
5
6
7
jobs:  
  My-Gitea-Actions:  
    runs-on: ubuntu-latest  
    # 此容器可使用docker,可查看 https://github.com/catthehacker/docker_images  
    container: catthehacker/ubuntu:act-latest  
    steps:  
      - run: docker version

使用缓存

似乎前面的设置cache没效果? 每次都是重新clone action

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
jobs:
  My-Gitea-Actions:
    runs-on: ubuntu-latest
    container: 
      image: catthehacker/ubuntu:act-latest
      # 方法二,手动指定持久化目录
      volumes:
        - ubuntu_hostedtoolcache:/opt/hostedtoolcache
    env:
      # 方法一,指定容器将工具缓存路径存放到 /toolcache ,该目录actRunner会默认持久化它
      RUNNER_TOOL_CACHE: /toolcache
    steps:
      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '17'

      - run: java -version

并行任务

1
2
3
runner:
  # 修改此数字,4表示同时支持4个任务并行,数量最好根据你机器性能和所跑任务负载统一决定,并不是越高越好
  capacity: 4
build with Hugo, theme Stack, visits 0