【Gitea Action 第一篇】上手体验
本文介绍了 Gitea Action,它是 Gitea 的内置 CI/CD 解决方案。文章涵盖了环境预览、启用 Action 功能、安装和注册 Runner 以及编写和运行 Actions 的步骤。通过简单的示例,读者可以了解如何在 Gitea 上使用 Actions 进行持续集成和持续部署。
一、前言
从 Gitea 1.19 版本开始,Gitea Action 成为了内置的 CI/CD 解决方案。设计上与 GitHub Actions 相似且兼容,依托于 act_runner (A runner for Gitea based on act)实现本地运行工作流。目前官方声明该模块仍是开发状态。
二、环境预览
Gitea 以及相关程序都使用虚拟机中的 Docker 运行。
- centos 7.9 虚拟机
- Gitea 1.19.3
- Docker Engine - Community 24.0.2
三、上手体验
1. 开启 Action 功能
默认情况下,Actions 功能是禁用的,需要修改 Gitea 配置文件 app.ini
,添加以下内容以启用它:
[actions]
ENABLED=true
重新应用配置后,可点击【首页右上角头像 — 管理后台】,确认 Runners 菜单页是否出现。
2. 安装&注册 Runner
-
获取 runner 注册令牌,点击【首页右上角头像 — 管理后台 — Runners — 创建 Runner】:
-
使用 docker 安装 runner
# 拉取最新镜像,latest为最新稳定版,nightly为最新夜间构建版,我此处选择 docker pull gitea/act_runner:nightly
-
准备挂载目录
# 挂载目录按自己需求自定 mkdir -p /opt/gitea/gitea-act-runner/data
-
准备 Runner 配置文件:
a. 在挂载目录下准备配置文件
# 进入挂载目录 cd /opt/gitea/gitea-act-runner # 创建配置文件 touch config.yaml
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:
-
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
/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
-
检查运行状态
# 查看运行日志,控制台有打印 success 日志 docker logs -f gitea-runner
以及检查 Gitea Runner 页面,查看是否有相关条目:
3. 仓库启用 Actions
后面我将以 action-demo
仓库为例,展示如何设置仓库并运行 Demo 示例。
-
进入
action-demo
仓库设置页面,并找到 Actions 启用配置,记得点击保存: -
检查 Actions 页面
4. 编写 Actions
我们需要通过工作流语法,编写实施文件,文件扩展名要求为 .yaml
,并存放在 .gitea/workflows/
目录下,Gitea 会扫描该目录中的文件,生成对应的工作流任务。
本篇我们只演示一个简单的 Actions 示例,更多的语法学习,可查阅工作流语法。
-
创建目录以及任务文件
-
编写
hello-world-action.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@v3 - 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 }}."
-
提交并推送代码
5. 查看 Action 运行
进入仓库页面 — Actions,查看运行的工作流。
点击工作流,可查看详情。
结束
感谢阅读本篇关于 Gitea Action 的介绍和上手体验。希望这篇文章对您理解和使用 Gitea Action 提供了一些帮助。如果您对 Gitea Action 有任何疑问或想要讨论更多相关话题,欢迎在下方留言,我们可以互相学习和分享经验。祝您在使用 Gitea Action 进行 CI/CD 工作流时取得成功!
更多的实际使用场景(相当多的坑)如:结合开发流程完成 CI 构建、代码质量审查等请关注后续文章。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)