自动化
Codex App 的自动化功能
概述
自动化功能允许你设置规则,让 Codex 自动执行常见任务。
自动化类型
1. 文件自动化
基于文件变化的自动化:
当文件变化时:
- 自动格式化
- 运行测试
- 生成文档2. 时间自动化
定时执行的任务:
每天上午 9 点:
- 检查依赖更新
- 运行完整测试
- 生成报告3. 事件自动化
响应特定事件:
当创建 PR 时:
- 自动审查
- 运行测试
- 通知团队创建自动化
可视化创建
- 点击"自动化"标签
- 点击"新建自动化"
- 选择触发器
- 配置动作
- 保存
配置示例
yaml
name: 自动测试
description: 文件变化时运行测试
trigger:
type: file-change
pattern: src/**/*.ts
actions:
- type: run-command
command: npm test
- type: notify
channel: slack内置自动化
1. 自动保存
启用:设置 → 编辑 → 自动保存
延迟:5 秒2. 自动格式化
启用:设置 → 编辑 → 保存时格式化
格式化器:Prettier3. 自动审查
启用:设置 → Git → PR 自动审查
范围:所有 PR自定义自动化
示例 1:自动文档
yaml
name: 自动文档
trigger:
type: file-change
pattern: src/**/*.py
contains: "def "
actions:
- type: codex
prompt: "为这个函数生成文档"
- type: write
path: docs/api.md示例 2:代码质量检查
yaml
name: 代码质量
trigger:
type: before-commit
actions:
- type: run-command
command: npm run lint
- type: run-command
command: npm test
- type: block
condition: lint-failed or test-failed示例 3:依赖更新
yaml
name: 依赖检查
trigger:
type: schedule
cron: "0 9 * * 1" # 每周一上午 9 点
actions:
- type: run-command
command: npm outdated
- type: codex
prompt: "检查是否有重要安全更新"
- type: notify
if: security-updates-found自动化动作
可用动作
| 动作 | 说明 |
|---|---|
| run-command | 运行命令 |
| codex | 调用 Codex |
| write | 写入文件 |
| notify | 发送通知 |
| git | Git 操作 |
| http | HTTP 请求 |
| delay | 延迟执行 |
| condition | 条件判断 |
动作组合
yaml
actions:
- type: codex
prompt: "检查代码质量"
- type: condition
if: issues-found
then:
- type: notify
message: "发现代码质量问题"
- type: git
action: create-commit
message: "自动修复代码问题"触发器
文件触发器
yaml
trigger:
type: file-change
pattern: src/**/*.ts
event: create | modify | delete时间触发器
yaml
trigger:
type: schedule
cron: "0 */4 * * *" # 每 4 小时Git 触发器
yaml
trigger:
type: git
event: push | pull-request | merge
branch: main条件判断
基础条件
yaml
- type: condition
if: file-size > 1000
then:
- type: notify
message: "文件过大"复合条件
yaml
- type: condition
if: test-failed AND coverage < 80
then:
- type: block
- type: notify监控和管理
查看历史
自动化 → 历史记录
查看执行记录和结果启停控制
自动化列表 → 切换开关
临时禁用自动化错误处理
yaml
on-error:
- type: notify
channel: slack
- type: log
level: error最佳实践
1. 渐进式启用
先在小范围测试:
1. 个人项目测试
2. 非关键流程
3. 全面推广2. 添加通知
重要自动化要有通知:
yaml
on-complete:
type: notify
summary: true3. 设置超时
防止无限循环:
yaml
timeout: 300 # 5 分钟故障排除
常见问题
| 问题 | 解决方案 |
|---|---|
| 不触发 | 检查触发条件 |
| 执行失败 | 查看错误日志 |
| 重复执行 | 检查触发器配置 |
查看日志
自动化 → 选择任务 → 查看日志