[英]How can I read all the branches in a GitHub repo using the Git API from CI?
Using the Git API in a CI system (eg. GitHub Actions or Travis-CI), I want to gather information on all the branches of the repo.在 CI 系统(例如 GitHub Actions 或 Travis-CI)中使用Git API ,我想收集有关 repo 的所有分支的信息。
Sadly, it appears that GitHub branches, unlike local branches, are isolated one from each other.遗憾的是,GitHub 分支似乎与本地分支不同,它们彼此隔离。
Let's say I have a repository with three branches ( master
and other two created from master
):假设我有一个包含三个分支的存储库( master
和其他两个从master
创建的):
If I run the following script:如果我运行以下脚本:
#!/usr/bin/env bash
printf "\n$ git for-each-ref --format='%(refname)' \n"
printf "$(git for-each-ref)\n"
printf "__________________________________________\n"
printf "\n$ git branch -a\n"
printf "$(git branch -a)\n"
I can only see master
, not the other two branches:我只能看到master
,而不是其他两个分支:
Is there any way to read all the GitHub branches with the Git API, or I'm forced to use the GitHub API ?有没有办法用 Git API 读取所有 GitHub 分支,或者我被迫使用 GitHub API ?
I hoped to be able to read at least the branches generated from the branch I'm on ( master
, in this case).我希望至少能够读取从我所在的分支(在本例中为master
)生成的分支。 I'm starting to guess that GitHub keeps that information for itself, without disclosing it in any canonical Git way...我开始猜测 GitHub 会为自己保留这些信息,而不会以任何规范的 Git 方式公开它......
You have to perform a full repository checkout in your workflow job in order to get the full repository data (including all branches).您必须在工作流作业中执行完整的存储库检出才能获得完整的存储库数据(包括所有分支)。 When you do git clone...
(without any switches like --depth
or --single-branch
) on your local machine you are getting all the repository data (tags, branches, commit history, etc.).当您在本地机器上执行git clone...
(没有任何开关,如--depth
或--single-branch
)时,您将获得所有存储库数据(标签、分支、提交历史等)。 This is why git branch -r
command output contains all the remote branches.这就是为什么git branch -r
命令输出包含所有远程分支的原因。
In GitHub Actions (or mentioned Travis CI) the clone operation behaves slightly different.在 GitHub Actions(或提到的 Travis CI)中,克隆操作的行为略有不同。 By default, to conserve bandwidth and disk storage space the CI system retrieves only the minimal required part of the repository in order to execute the workflow.默认情况下,为了节省带宽和磁盘存储空间,CI 系统仅检索存储库所需的最少部分以执行工作流。 This usually is only the current branch associated with the workflow and limited commit history.这通常只是与工作流和有限提交历史相关联的当前分支。 This is why your commands do not return all the expected branches.这就是您的命令不返回所有预期分支的原因。 However, you can override the default checkout behaviour.但是,您可以覆盖默认的结帐行为。 If you look at the actions/checkout
docs :如果您查看actions/checkout
文档:
Only a single commit is fetched by default, for the ref/SHA that triggered the workflow.对于触发工作流的 ref/SHA,默认情况下仅获取单个提交。 Set
fetch-depth: 0
to fetch all history for all branches and tags .设置fetch-depth: 0
以获取所有分支和标签的所有历史记录。
Taking the above into consideration, to fetch all the branches, you have to adjust the actions/checkout@v2
step in your workflow according to the docs :考虑到上述情况,要获取所有分支,您必须根据文档调整工作流程中的actions/checkout@v2
步骤:
Fetch all history for all tags and branches获取所有标签和分支的所有历史记录
- uses: actions/checkout@v2 with: fetch-depth: 0
Now, when you call in the next step sth.现在,当您调用下一步时。 like:喜欢:
- name: List all the remote branches
run: git branch -r
you should get the expected output.你应该得到预期的输出。
Speaking about Travis CI.说到 Travis CI。 The documentation is also clear about this (see §Git Clone Depth ).文档对此也很清楚(参见§Git Clone Depth )。 You have to adjust your travis.yml
to disable --depth
flag:您必须调整您的travis.yml
以禁用--depth
标志:
git:
depth: false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.