GitHub Actions Rest API#

GitHub 的 API 是市面上做的最好的一批之一. 也是最早一批使用 GraphQL API 的公司.

你可以用 GitHub API 来操作 Workflow. 例如创建一个 Workflow dispatch event.

使用 API 是要鉴权的, 最佳的做法是创建一个 Personal Access Token. GitHub API 是一个 HTTP request API, 你可以用 Python 的 request 库来做. 不过我推荐使用 PyGitHub 库, 用人类友好的方式对这些方法进行了封装. 下面给出了一个手动运行 Workflow 的一个脚本.

 1# -*- coding: utf-8 -*-
 2
 3"""
 4Reference:
 5
 6- PyGitHub: https://pygithub.readthedocs.io/en/stable/introduction.html
 7"""
 8
 9from pathlib import Path
10
11from github import Github, Auth
12from rich import print as rprint
13
14token = Path.home().joinpath(".alfred-afwf_github", "default").read_text()
15auth = Auth.Token(token)
16gh = Github(auth=auth)
17repo = gh.get_repo("MacHu-GWU/learn_github_action-project")
18workflow = repo.get_workflow("01_02_minimal_workflow.yml")
19workflow.create_dispatch(ref="main")

Reference: