当前位置:   article > 正文

【Gitea Action 第一篇】上手体验_gitea actions

gitea actions

一、前言

从 Gitea 1.19 版本开始,Gitea Action 成为了内置的 CI/CD 解决方案。设计上与 GitHub Actions 相似且兼容,依托于 act_runner (A runner for Gitea based on act)实现本地运行工作流。目前官方声明该模块仍是开发状态。

二、环境预览

Gitea 以及相关程序都使用虚拟机中的 Docker 运行。

  1. centos 7.9 虚拟机
  2. Gitea 1.19.3
  3. Docker Engine - Community 24.0.2

三、上手体验

1. 开启 Action 功能

默认情况下,Actions 功能是禁用的,需要修改 Gitea 配置文件 app.ini ,添加以下内容以启用它:

[actions]
ENABLED=true
  • 1
  • 2

重新应用配置后,可点击【首页右上角头像 — 管理后台】,确认 Runners 菜单页是否出现。

2. 安装&注册 Runner

  1. 获取 runner 注册令牌,点击【首页右上角头像 — 管理后台 — Runners — 创建 Runner】:
    在这里插入图片描述

  2. 使用 docker 安装 runner

    # 拉取最新镜像,latest为最新稳定版,nightly为最新夜间构建版,我此处选择
    docker pull gitea/act_runner:nightly
    
    • 1
    • 2
  3. 准备挂载目录

    # 挂载目录按自己需求自定
    mkdir -p /opt/gitea/gitea-act-runner/data
    
    • 1
    • 2
  4. 准备 Runner 配置文件:

    a. 在挂载目录下准备配置文件

    # 进入挂载目录
    cd /opt/gitea/gitea-act-runner
    
    # 创建配置文件
    touch config.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5

    b. 编辑配置文件,将下述内容复制进去

    # 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:
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  5. docker 运行,请按需修改对应项

    docker run \
        -v /opt/gitea/gitea-act-runner/config.yaml:/config.yaml \
        -v /opt/gitea/gitea-act-runner/data:/data \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -e CONFIG_FILE=/config.yaml \
        -e GITEA_INSTANCE_URL=http://192.168.1.43:3000 \
        -e GITEA_RUNNER_REGISTRATION_TOKEN=VRitMSZzktPhoIH46SZRqWC0TjvJnRoqBbQHVfWB \
        --name gitea-runner \
        -d gitea/act_runner:nightly
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • /opt/gitea/gitea-act-runner/config.yaml:请修改为你宿主机配置文件所在目录
    • /opt/gitea/gitea-act-runner/data:请修改为你宿主机 data 挂载目录
    • CONFIG_FILE:指定启动时配置文件
    • GITEA_INSTANCE_URL:Gitea 实例访问地址
    • GITEA_RUNNER_REGISTRATION_TOKEN:第1小点复制的 token
  6. 检查运行状态

    # 查看运行日志,控制台有打印 success 日志
    docker logs -f gitea-runner
    
    • 1
    • 2

    以及检查 Gitea Runner 页面,查看是否有相关条目:

    在这里插入图片描述

3. 仓库启用 Actions

后面我将以 action-demo 仓库为例,展示如何设置仓库并运行 Demo 示例。

  1. 进入 action-demo 仓库设置页面,并找到 Actions 启用配置,记得点击保存:

    在这里插入图片描述

  2. 检查 Actions 页面

    在这里插入图片描述

4. 编写 Actions

我们需要通过工作流语法,编写实施文件,文件扩展名要求为 .yaml ,并存放在 .gitea/workflows/ 目录下,Gitea 会扫描该目录中的文件,生成对应的工作流任务。

本篇我们只演示一个简单的 Actions 示例,更多的语法学习,可查阅工作流语法

  1. 创建目录以及任务文件

    在这里插入图片描述

  2. 编写 hello-world-action.yaml 文件,本文拿官方例子进行演示,内容为:

    name: Gitea Actions Demo
    run-name: ${{ gitea.actor }} is testing out Gitea Actions 
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/185996
推荐阅读
相关标签