PyGithub g.get_repo()本地正常但在GitHub托管及自托管Runner报错
解决GitHub Actions中PyGithub 404 Not Found问题
排查与解决步骤
确认Token权限范围
检查你的token是否包含repo权限(私有仓库必填),同时确认组织未对Actions环境的请求设置额外限制。可在GitHub的「Settings > Developer settings > Personal access tokens」页面查看权限详情。验证仓库名称准确性
确保repo_name的组织名、仓库名拼写完全正确,注意大小写(尽管GitHub本身不区分,但API请求偶尔会因大小写匹配异常报错)。可以直接在浏览器中访问https://api.github.com/repos/customer-sandbox/some-repo,并在请求头中携带你的token,验证是否能返回仓库信息。规范Actions中Token的传递方式
禁止在代码中硬编码token,改用环境变量传递:- 在仓库「Settings > Secrets and variables > Actions」中添加名为
GH_TOKEN的密钥,值为你的有效token。 - 修改代码读取环境变量:
import os from github import Github token = os.getenv("GH_TOKEN") repo_name = "customer-sandbox/some-repo" g = Github(token) repo = g.get_repo(repo_name) print(repo) - 在Workflow文件中配置环境变量:
steps: - name: Run script env: GH_TOKEN: ${{ secrets.GH_TOKEN }} run: python your_script.py
- 在仓库「Settings > Secrets and variables > Actions」中添加名为
自托管Runner网络检查
如果使用自托管Runner,确认Runner所在网络可正常访问api.github.com,无防火墙、代理拦截。可在Runner上执行以下命令测试连通性:curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/repos/customer-sandbox/some-repo更换有效Token测试
重新生成一个具备必要权限的新token,替换原有token后在Actions中测试,排除token过期或被撤销的可能。
内容的提问来源于stack exchange,提问作者Underoos
相关产品推荐
相关产品推荐

