跳到主要内容

触发事件

本文档是 AtomGit Action 触发事件的完整参考,包含 push、pull_request、workflow_dispatch、schedule 等所有事件的配置语法和过滤字段说明。

工作流通过 on 关键字定义触发条件。AtomGit Action 支持以下触发事件,工作流文件存放于仓库的 .gitcode/workflows/ 目录下。

1.1 push

当发生推送操作时触发,包括分支推送和标签推送。

on:
push:
branches:
- main
- 'releases/**'
tags:
- v1.*
- v2.*
paths:
- 'src/**'
- 'package.json'

过滤字段:

字段说明示例
branches匹配的分支名模式main, releases/**
branches-ignore排除的分支名模式experimental/**
tags匹配的标签名模式v1.*
tags-ignore排除的标签名模式v1.0.*
paths匹配的文件路径模式src/**
paths-ignore排除的文件路径模式docs/**

注意: branchesbranches-ignore 不可同时使用;tagstags-ignore 不可同时使用。pathspaths-ignore 可以与分支/标签过滤组合使用。

特殊用法:

on:
push:
branches:
- '**' # 所有分支
- '!main' # 推除 main(排除模式以 ! 开头)

1.2 pull_request

当创建、更新或合并 Pull Request 时触发。

on:
pull_request:
types:
- open
- reopen
- update
- merge
branches:
- main
- 'feature/**'
paths:
- 'src/**'
paths-ignore:
- 'docs/**'

事件类型(types):

类型说明
openPR 创建
reopenPR 重新打开
updatePR 源分支有新提交(最常见的触发场景)
mergePR 合并

默认值: 不指定 types 时,默认为 [open, reopen, update],即 PR 创建、重新打开和更新时触发,不包含合并事件。

过滤字段: 同 push,支持 branches, branches-ignore, paths, paths-ignore

1.3 pull_request_target

pull_request 类似,但工作流运行在目标分支(base 分支)的上下文中,可读写目标仓库。适用于需要访问仓库 Secrets 或进行写操作的场景(如自动标签、评论等)。

on:
pull_request_target:
types:
- open
- update
- merge
branches:
- main

安全提示: pull_request_target 会使用目标分支的 workflow 文件和权限, fork 仓库的 PR 也能触发。请谨慎处理 fork PR 的代码执行,避免安全风险。

默认值: 不指定 types 时,默认为 [open, reopen, update],与 pull_request 一致。

1.4 issue_comment

当 Issue 的评论被创建、编辑或删除时触发。

on:
issue_comment:
types:
- created
- edited
- deleted

事件类型:

类型说明
created评论创建
edited评论编辑
deleted评论删除

1.5 pull_request_comment

区别于 issue_comment,仅在 Pull Request 评论时触发。

on:
pull_request_comment:
types:
- created
- edited
- deleted
branches:
- main
comments:
- '/deploy'
- '/test'

事件类型:

类型说明
created评论创建
edited评论编辑
deleted评论删除

过滤字段:

字段说明示例
branches匹配 PR 目标分支名模式main, feature/**
comments基于正则表达式的评论内容过滤/deploy, /test

comments 过滤说明: comments 字段支持对评论内容基于正则表达式进行条件过滤,只有评论内容匹配指定正则模式的才会触发工作流。例如配置 comments: ['/deploy'],则只有评论中包含 /deploy 指令时才触发。

1.6 workflow_dispatch

手动触发工作流,支持自定义输入参数。

on:
workflow_dispatch:
inputs:
environment:
description: '部署目标环境'
required: true
default: 'staging'
type: string
deploy_count:
description: '并行部署数量'
required: false
default: "1"
type: number
dry_run:
description: '是否仅验证不部署'
required: false
default: "false"
type: boolean
log_level:
description: '日志级别'
required: false
default: 'info'
type: choice
options:
- 'info'
- 'error'
- 'warning'

inputs 属性字段:

字段说明示例值
typeinputs类型string , boolean, choice , number
required是否必填true, false
description描述env_type, port_number
default默认值true, false
options单选项,仅适用于 choice类型info, warning, error

AtomGit Action 的 inputs 支持 string , boolean ,choice , number 类型。

type说明示例值
string字符串输入test, prod, dev
boolean布尔值输入true, false
choice单选输入,通过options字段配置选项info, warning, error
number数字输入8080, 50

在工作流中通过 inputs 上下文访问输入值,如 ${{ inputs.environment }}。如需数字或布尔语义,可在工作流中通过表达式进行类型转换。

1.8 workflow_call

允许一个工作流被另一个工作流调用(可复用工作流)。

on:
workflow_call:
inputs:
config-path:
description: '配置文件路径'
required: false
default: 'config/default.json'
type: string
environment:
description: '部署环境'
required: true
type: string
secrets:
deploy-token:
description: '部署认证令牌'
required: true
db-password:
description: '数据库密码'
required: false

调用方工作流示例:

jobs:
deploy:
name: 部署
uses: ./.gitcode/workflows/deploy.yml
with:
config-path: 'config/production.json'
environment: production
secrets:
deploy-token: ${{ secrets.DEPLOY_TOKEN }}
db-password: ${{ secrets.DB_PASSWORD }}

1.9 schedule

定时触发工作流,使用 POSIX cron 语法。

on:
schedule:
- cron: '30 5 * * 1,3' # 每周一、三 05:30 UTC
- cron: '0 2 * * *' # 每天 02:00 UTC
- cron: '15 0 1 1 *' # 每年1月1日 00:15 UTC

cron 语法格式: 分钟 小时 日 月 星期

位置范围说明
分钟0-59每小时的第几分钟
小时0-23UTC 时间小时
1-31每月的第几天
1-12月份
星期0-60=周日, 1=周一, ..., 6=周六

特殊符号:

符号说明示例
*任意值* * * * * — 每分钟
,列表分隔1,3,5 — 第1、3、5
-范围1-5 — 1到5
/步长*/15 — 每15单位

注意: schedule 的最短间隔为 5 分钟。cron 使用 UTC 时间,请换算为本地时间。