概念澄清与总体思路 “Ubuntu Trigger”并非 Ubuntu 官方标准组件的名称,常见有两种语境:其一是某些教程或第三方工具对“触发器”概念的泛称;其二是 Kubernetes 上的 Tekton Triggers(在集群中基于事件触发 PipelineRun/TaskRun)。无论哪种语境,实现自动化测试的思路都是:定义“触发器”→绑定“要执行的测试脚本/命令”→在触发时运行测试并产出报告→必要时做通知与收敛。若你在单机 Ubuntu 上找不到名为“ubuntu-trigger”的包,可直接采用下文更通用的替代方案。
单机 Ubuntu 的落地做法
Kubernetes 环境的 Tekton Triggers 做法
实践示例
示例一 文件变更触发本地测试(单机 Ubuntu)
#!/usr/bin/env bash
set -euo pipefail
LOG="test-$(date +%F-%H%M%S).log"
echo "[$(date)] Start tests" | tee "$LOG"
pytest tests/ --junitxml=reports/results.xml >> "$LOG" 2>&1 || { echo "Tests failed"; exit 1; }
echo "[$(date)] Tests passed" | tee -a "$LOG"
chmod +x run_tests.sh && mkdir -p reports
#!/usr/bin/env bash
SRC_DIR="src"
while inotifywait -r -e modify,create,delete "$SRC_DIR"; do
echo "[$(date)] Change detected, running tests..."
./run_tests.sh
done
可用 nohup 或 systemd 将该监听脚本作为服务常驻运行。
示例二 Tekton Triggers 触发测试运行(Kubernetes)
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: run-pytest
spec:
steps:
- name: test
image: python:3.11-slim
script: |
pip install -r requirements.txt pytest
pytest tests/ --junitxml=/workspace/results.xml
volumeMounts:
- name: output
mountPath: /workspace
volumes:
- name: output
emptyDir: {}
---
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: pytest-run-
spec:
taskRef:
name: run-pytest
podTemplate:
volumes:
- name: output
emptyDir: {}
workspaces:
- name: output
emptyDir: {}
工具选择与对比
| 方案 | 适用环境 | 触发源 | 优点 | 局限 |
|---|---|---|---|---|
| 单机脚本 + inotify/cron | 本机或虚拟机 | 文件变更、定时、登录 | 简单、无额外依赖、易调试 | 横向扩展与统一治理较弱 |
| Tekton Triggers | Kubernetes 集群 | webhook、事件 | 云原生、可编排、可观测与权限细粒度 | 架构与运维复杂度更高 |
| GitHub Actions/GitLab CI | 托管或自托管 Runner | push、PR、定时 | 配置即代码、生态完善 | 需将代码与流水线托管在对应平台 |
若你只是想在提交代码后自动跑测试,优先考虑 GitHub Actions/GitLab CI 或 Jenkins;若在集群内做标准化交付与治理,选择 Tekton Triggers 更合适。