# 6-gitea action [Act Runner | Gitea Documentation](https://docs.gitea.cn/usage/actions/act-runner) [Gitea Actions 搭建 - Seepine's Blog](https://seepine.com/git/gitea/actions/) ## 安装 ### gitea开启功能 * gitea开启 功能, 路径`data/gitea/conf/app.ini` ``` [actions] ENABLED=true ``` 重启 ### 获取令牌 * 获取令牌 * 实例级别:管理员设置页面,例如 `/admin/actions/runners`。 * 组织级别:组织设置页面,例如 `//settings/actions/runners`。 * 存储库级别:存储库设置页面,例如 `///settings/actions/runners` ### 安装 act_runner * 安装act_runner ```sh # 下载 docker pull gitea/act_runner:latest # 生成配置文件 docker run --entrypoint="" --rm -it gitea/act_runner:latest act_runner generate-config > config.yaml ``` ```yaml 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 ``` ### 标签 查看 `config.yaml` 配置文件 ```yaml labels: - "ubuntu-latest:docker://gitea/runner-images:ubuntu-latest" - "ubuntu-latest-slim:docker://gitea/runner-images:ubuntu-latest-slim" - "ubuntu-22.04:docker://gitea/runner-images:ubuntu-22.04" - "ubuntu-20.04:docker://gitea/runner-images:ubuntu-20.04" ``` 这些表示`runs-on`可以选择的容器以及映射关系, 例如 `ubuntu-latest` 将在 `gitea/runner-images:ubuntu-latest` 中运行 最新的镜像为: https://hub.docker.com/r/gitea/runner-images, slim: 未安装python,git似乎也没有 如果要添加, 需要修改上面生成的config.yaml 可以手动下载 对应的镜像, 防止 action 运行时下载失败 ```sh docker pull gitea/runner-images:ubuntu-latest-slim # 只有60M docker pull gitea/runner-images:ubuntu-latest # 400多M ``` ## 快速使用 * 储库的设置页面, 启用action, 更新仓库设置 * 新建`.gitea/workflows/`文件夹, 新增`demo.yaml` 这是官方给的案例, 实际需要修改 ```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@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的 ```yaml 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 ```yaml 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 修改配置文件 ```sh cache: enabled: true dir: "" # 使用步骤 1. 获取的 LAN IP host: "172.17.0.1" # 使用步骤 2. 使用空闲的端口 port: 8088 ``` 这里的设置了8088, 所以前面端口映射就是8088 ## 默认上下文 在编写步骤文件时,可以直接使用默认的变量来实现想要的功能,语法为 `${{ xxx }}`,具体有哪些变量可查看[Github Actions Context Docs](https://docs.github.com/zh/actions/learn-github-actions/contexts#github-context) ```sh - run: echo ${{ github.ref }} - run: echo ${{ github.repository }} ``` ```sh refs/heads/main seepine/actions-demo ``` ## 环境变量 环境变量分为默认环境变量和自定义环境变量,语法为 `${{ env.xxx }}`,具体请查看[Github Actions Variables Docs](https://docs.github.com/zh/actions/learn-github-actions/variables#default-environment-variables) ```sh 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 }} ``` ```sh seepine/actions-demo custom env value asdf1234 ``` ## Secrets变量 一般用于定义密码等敏感变量,此变量输出时会变成*,但不影响使用,在设置-Secrets中添加Key-Value即可 ``` - run: echo ${{ secrets.CUSTOM_KEY }} ``` ## output 许多时候我们会需要输出一些特定内容供他人获取,若输出到环境变量,我们很难随心定义key,因为有可能会与其他步骤的环境变量冲突而覆盖它,因此出现了output这个用法,最常见的即 [Docker metadata](https://github.com/docker/metadata-action) ```sh 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 ```sh jobs: My-Gitea-Actions: runs-on: linux_runner runs-on: windows_runner ``` ## 指定action的地址 默认是github仓库, 但可能连不上 ``` uses: https://github.com/my_custom/other-action@v2 ``` 也可以通过修改gitea的`app.ini`配置,改为从相应的仓库下载 ```sh [actions] # 1.19 可直接填写任意url如:https://github.com # 1.20起,不填默认从 github,填self表示从自建仓库下载 DEFAULT_ACTIONS_URL = self ``` ## 指定运行容器的镜像 `gitea act_runner` 默认运行镜像是 `node:16-bullseye` ,并没有 docker 环境 ```yaml 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 ```yaml 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 ``` ## 并行任务 ```yaml runner: # 修改此数字,4表示同时支持4个任务并行,数量最好根据你机器性能和所跑任务负载统一决定,并不是越高越好 capacity: 4 ```