下面是一个 GitHub Action 的示例:
name: GitHub Actions Demo
on: [push, pull_request]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "Hello World!"
在 on 关键字下,定义的就是触发 Workflow 执行的事件。下面常用的几种 GitHub Action 事件:
workflow_dispatch
, 手动触发在 inputs 中可以添加交互参数(可选)。
on:
workflow_dispatch:
inputs:
name:
description: 'Person to greet'
required: true
default: 'Mona the Octocat'
on:
push
on:
issues:
types: [opened, edited, milestoned]
issue_comment
, issue 评论on:
issue_comment:
types: [created, deleted]
on:
project:
types: [created, deleted]
pull_request
, pr 生命周期on:
pull_request:
types: [assigned, opened, synchronize, reopened]
利用这些事件 hook,可以自动化很多流程。
访问链接页面 https://github.com/settings/tokens/new 申请一个 Token。
需要勾选 repo 权限。
在仓库 https://github.com/shaowenchen/wait-webhook-to-run 下,新建一个文件 .github/workflows/worker.yml
。内容如下:
on:
repository_dispatch:
types:
- webhook-1
- webhook-2
jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Hello World
run: |
echo Hello World!
在 repository_dispatch
的 types
中,可以自定义事件类型。
下面是 API 调用格式:
curl -X POST https://api.github.com/repos/:owner/:repo/dispatches \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Authorization: token TRIGGER_TOKEN" \
--data '{"event_type": "TRIGGER_EVENT"}'
其中,owner
是用户名,repo
是仓库名, TRIGGER_TOKEN
是上面申请的 Token 凭证,TRIGGER_EVENT
是自定义的事件名。
webhook-1
事件curl -X POST https://api.github.com/repos/shaowenchen/wait-webhook-to-run/dispatches \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxx" \
--data '{"event_type": "webhook-1"}'
webhook-2
事件curl -X POST https://api.github.com/repos/shaowenchen/wait-webhook-to-run/dispatches \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Authorization: token ghp_xxxxxxxxxxxxxxxxxxxxxxxxxx" \
--data '{"event_type": "webhook-2"}'
查看 GitHub Action 执行: