Git 2.27(2020 年第二季度)展示了另一种方法:用户可以指定要构建的哪些分支,而不是总是通过 Actions 在 GitHub 上构建所有分支。
参见Jeff King (peff) 的commit e76eec3(2020 年 5 月 7 日)。
(由 Junio C Hamano -- gitster -- 合并到 commit dd4a287,2020 年 5 月 13 日)
ci:允许 GitHub Actions 的每个分支配置
签字人:杰夫·金
根据各个开发人员的工作流程,我们的 GitHub Actions CI 作业在每个分支上运行可能很方便,也可能很烦人。
作为一个烦人的例子:如果你携带许多半完成的工作分支并经常将它们与 master 进行 rebase,你会得到大量不有趣的失败报告(更不用说浪费的 CPU) .
此提交添加了一个新作业,该作业检查存储库中的一个特殊分支以获取 CI 配置,然后运行它找到的 shell 脚本来决定是否跳过其余的测试。
如果缺少该分支或脚本,默认将继续为所有 ref 运行测试。
已经讨论了一些替代方案:
一种选择是在提交本身中携带有关是否应该对其进行测试的信息,无论是在树本身(更改工作流程 YAML 文件)还是在提交消息中(“[skip ci]”标志或类似标志)。但是这些使用起来令人沮丧且容易出错:
- 您必须手动将它们应用于您要标记的每个分支
- 他们很容易泄漏到其他工作流程中,例如通过电子邮件发送补丁
我们同样可以尝试从分支名称中获取一些信息。但这导致了关于默认值应该是“关闭”还是“打开”的争论,并且覆盖最终仍然有些尴尬。
如果我们默认为“on”,您必须记住适当地命名您的分支以跳过 CI。
如果“关闭”,您最终将不得不扭曲您的分支名称或使用额外的 refspec 复制您的推送。
相比之下,此提交的解决方案让您只需指定一次配置就可以忘记它,并且所有数据都在其自己的 ref 中关闭,可以通过单独的 fork 更改而不接触主树。
有一些设计决策来自列表讨论。我在这里总结一下:
我们可以使用 GitHub 的 API 来检索配置引用,而不是真正的结帐(然后只需通过一些 javascript 对其进行操作)。
无论哪种方式,我们仍然需要启动一个 VM 并通过网络与 GitHub 联系,因此最终速度不会快多少。
我选择使用 shell 来保持与我们其他工具相似的东西(并且真的可以用任何你想要的语言实现 allow-refs)。这也便于在本地测试您的脚本,并在普通 git.git 树的上下文中对其进行修改。
我们可以将众所周知的引用名排除在refs/heads/ 之外,以避免混淆分支命名空间。但这使得操纵起来很尴尬。
相比之下,您只需“git checkout ci-config”即可进行更改。
-
我们可以假设 ci-config 引用除了配置(即与 git.git 的其余部分无关的分支)之外什么都没有。
但是处理孤儿分支很尴尬。相反,我们将尽最大努力使用浅部分克隆仅有效地检查 ci/config 目录,这允许您的 ci-config 分支只是一个普通分支,而您的配置更改位于顶部。
我们可以提供一个更简单的界面,例如引用模式的静态列表。
但无论如何我们都无法摆脱启动整个 VM 的问题,因此我们不妨使用该功能使配置尽可能灵活。
如果我们添加更多配置,我们应该能够重用我们的部分克隆来设置更多输出。
所以脚本是ci/config/allow-refs.sample:
#!/bin/sh
#
# Sample script for enabling/disabling GitHub Actions CI runs on
# particular refs. By default, CI is run for all branches pushed to
# GitHub. You can override this by dropping the ".sample" from the script,
# editing it, committing, and pushing the result to the "ci-config" branch of
# your repository:
#
# git checkout -b ci-config
# cp allow-refs.sample allow-refs
# $EDITOR allow-refs
# git commit -am "implement my ci preferences"
# git push
#
# This script will then be run when any refs are pushed to that repository. It
# gets the fully qualified refname as the first argument, and should exit with
# success only for refs for which you want to run CI.
case "$1" in
# allow one-off tests by pushing to "for-ci" or "for-ci/mybranch"
refs/heads/for-ci*) true ;;
# always build your integration branch
refs/heads/my-integration-branch) true ;;
# don't build any other branches or tags
*) false ;;
esac
the action .github/workflows 所要做的就是
即:
git -c protocol.version=2 clone \
--no-tags \
--single-branch \
-b ci-config \
--depth 1 \
--no-checkout \
--filter=blob:none \
https://github.com/${{ github.repository }} config-repo \
&& \
cd config-repo \
&& \
git checkout HEAD -- ci/config
这是:
enabled=yes
if test -x config-repo/ci/config/allow-ref &&
! config-repo/ci/config/allow-ref '${{ github.ref }}'
then
enabled=no
fi