如何开启 GitHubAction 的 workflow_dispatch 触发器
在工作流的 yml 文件定义的 on 中,提供workflow_dispatch触发器 【必须】
1 2 3 4 5 6 7 8 9
| name: 你的工作流名称
on: workflow_dispatch:
jobs: start:
|
开启workflow_dispatch后,你的工作流中会出现运行工作流的按钮,手动点击Run workflow按钮,将会执行一次工作流:

现在可以进入下一步,使用 cron 调度触发工作流的手动执行。
使用云函数提供 cron 调度服务
工具的选择上,人们总是会选择便捷且有效。如果恰好是免费的,那就大快人心。
我选择的 cron 调度是 腾讯云函数 SCF.。简单的 POST 一个链接,不会达到收费标准。
我们打开云函数的创建界面:
- 选择自定义创建
- 函数名称为默认的 helloworld-1623250991 ,可以自己输入名称。
- 地域使用任何地域即可,这里默认选择了广州
- 运行环境选择 Python3.6 (Python 2.7 版本也行)
- 提交方法选择在线编辑。
- 执行方法使用默认值:index.main_handler
- 函数代码如下:
Python 2.7 版本代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import requests def main_handler(event, context): r = requests.post("https://api.github.com/repos/xianrenqh/hui_blog_hexo/actions/workflows/23465728/dispatches", json = {"ref": "master"}, headers = {"User-Agent":'curl/7.52.1', 'Content-Type': 'application/json', 'Accept': 'application/vnd.github.v3+json', 'Authorization': 'token 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'}) if r.status_code == 204: return "This's OK!" else: return r.status_code
|
Python 3.6 版本代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import requests import json
def run(): payload = json.dumps({"ref": "master"}) header = {'Authorization': 'token GitHubToken', "Accept": "application/vnd.github.v3+json"} response_decoded_json = requests.post( f'https://api.github.com/repos/Github账号/Github项目名/actions/workflows/GithubAction工作流名称或ID/dispatches', data=payload, headers=header)
def main_handler(event, context): return run()
|
- 函数代码中的 GitHubToken、Github 账号、Github 项目名、GithubAction 工作流名称或 ID 需要根据自己的账号及项目填写。具体的 API 调用规则可参考: GithubDoc.
获取 GithubAction 工作流 ID 接口:
**url: **https://api.github.com/repos/你的用户名/你的项目名/actions/workflows
method: get
header:
Authorization |
token 你的 token |
Accept |
application/vnd.github.v3+json |
body: {ref:master’}
PHP 请求案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/你的用户名/你的项目名/actions/workflows'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ref:master'}'");
$headers = array(); $headers[] = 'User-Agent: Apipost client Runtime/+https://www.apipost.cn/'; $headers[] = 'Authorization: token 你的TOKEN'; $headers[] = 'Accept: application/vnd.github.v3+json'; $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch);
|
Python 请求案例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import requests
headers = { 'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/', 'Authorization': 'token 你的token', 'Accept': 'application/vnd.github.v3+json', 'Content-Type': 'application/json', }
data = '{ref:master}'
response = requests.post('https://api.github.com/repos/你的用户名/你的项目名/actions/workflows', headers=headers, data=data)
|
- 具体填写如下图所示:

- 在高级配置中,将执行超过时间设置为合适的时间,这里我设置为最大值900秒:

- 触发器配置选择默认流量 - 定时触发 - 自定义触发周期,并填入合适的 Cron 表达式,这里的Cron当前以 UTC +8 中国标准时间 (China Standard Time)运行,即北京时间。我输入的:
0 0 16 * * * *
表示了每天 16 点执行 1 次。

触发器也可以选择 api 调用,创建好后会生成一个 api url (webhook 地址)。粘贴到对应的位置调用即可。