丢弃Jenkins,轻量级CICD方案 -- Gitea Actions
前言
从 Gitea 1.19 版本开始,Gitea Action 成为了内置的 CI/CD 解决方案。设计上与 GitHub Actions 相似且兼容,依托于 act_runner(A runner for Gitea based on act)实现本地运行工作流。相比传统Jenkins CICD方案,Gitea Action使用YAML文件定义工作流,配置简单直观。容器化的Runner使其工作节点更加灵活,且支持异步CI/CD。无需再维护额外CI服务器,所有流程都在容器中进行,在基础镜像定制完整的前提下不受环境依赖影响。
使用Gitea Actions,需已安装Gitea环境,Gitea安装过程:Gitea 安装部署 (opsnote.top)
1. 获取 Runner 注册Token
管理后台
--> Runners
--> 创建Runner
,复制 REGISTRATION ToKEN
2. 创建 Runner 节点
2.1 创建 Runner 的 Docker-compose 文件,Runner 运行于容器中
mkdir -p /data/Act_runner/conf && cd /data/Act_runner && vim docker-compose.yml
version: "3.1"
services:
runner:
image: registry.cn-guangzhou.aliyuncs.com/hzbb/act_runner:0.2.11
restart: always
container_name: act-runner
environment:
CONFIG_FILE: /config.yaml
GITEA_INSTANCE_URL: "http://IP:23000/" # gitea仓库地址
GITEA_RUNNER_REGISTRATION_TOKEN: "***********" # REGISTRATION ToKEN
GITEA_RUNNER_NAME: "Runner-node1" # 节点名称
GITEA_RUNNER_LABELS: "${RUNNER_LABELS}"
volumes:
- ./conf/config.yaml:/config.yaml
- ./data:/data
- /var/run/docker.sock:/var/run/docker.sock
2.2 修改 Runner 配置
vim /data/Act_runner/conf/config.yaml
# 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
labels:
- "ubuntu-latest:docker://registry.cn-guangzhou.aliyuncs.com/hzbb/runner-images:ubuntu-latest"
- "ubuntu-22.04:docker://registry.cn-guangzhou.aliyuncs.com/hzbb/runner-images:ubuntu-22.04"
- "ubuntu-20.04:docker://registry.cn-guangzhou.aliyuncs.com/hzbb/runner-images:ubuntu-20.04"
- "maven-3.8:docker://registry.cn-guangzhou.aliyuncs.com/hzbb/runner-images:maven-v3.8.8"
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:
host:
workdir_parent: /
- 修改labels配置使其默认从阿里云仓库拉取镜像
2.3 运行 Runner 容器
cd /data/Act_runner && docker-compose up -d
此时可以看到 Runner 已经注册上来
3. 配置变量
我们执行CICD的过程中会用到一些自定义的变量,如镜像仓库地址、镜像仓库账号、镜像仓库密码等。如果同名变量存在于多个级别,则级别最低的变量优先。仓库级别的变量总是比组织或者用户级别的变量优先被选中。
设置
--> 变量
--> 添加变量
这个仓库使用的变量,在仓库设置中添加,如果是这个组织下所有的仓库都使用相同的变量,则在组织设置中添加变量即可。
4. JAVA应用CI测试
在仓库中新建一个 .gitea/workflows/java-ci.yaml
文件,内容如下
name: java ci
run-name: java ci
on:
release:
types: [published]
jobs:
java-ci:
runs-on: maven-3.8
steps:
- name: Checkout code
uses: https://gitea.com/actions/checkout@v3
- name: Build code
run: |
mvn clean install
- name: Build image
run: |
docker build -t ${{ vars.REGISTRY_URL }}/${{ gitea.repository }}:${{ gitea.ref_name }} .
- name: Push image
run: |
docker login -u ${{ vars.REGISTRY_USER }} -p ${{ vars.REGISTRY_PASSWORD }} ${{ vars.REGISTRY_URL }}
docker push ${{ vars.REGISTRY_URL }}/${{ gitea.repository }}:${{ gitea.ref_name }}
- name: echo info
run: |
echo "${{ gitea.repository }} 发布已完成!"
echo "镜像地址:${{ vars.REGISTRY_URL }}/${{ gitea.repository }}:${{ gitea.ref_name }} "
文件说明:
types: [published]
发生“版本发布”动作时,触发 Actionruns-on: maven-3.8
执行任务的镜像,与 Runner 配置文件相对应name: Checkout code
代码检出name: Build code
代码编译name: Build image
打包成镜像name: Push image
推送到镜像仓库name: echo info
输出完成信息
5. 版本发布
版本发布
--> 发布新版
6. 结果验证
- 任务执行过程
- 镜像仓库
写在最后,对于一些单体应用,使用这个方式来管理更为妥当。提交代码、发布版本后自动对外提供最新版本镜像。对于项目级别仓库,可在声明文件中加入单元测试、代码扫描、应用部署等流程。
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
运维小记!
喜欢就支持一下吧
打赏
微信
支付宝