我们的Bazel项目从托管在GitHub上的私有Git存储库中提取代码。
在某些环境中(碰巧所有环境都是macOS),我们希望使用SSH身份验证来访问存储库。因此,对于这些环境,我们可以在new_git_repository规则中指定remote = git@github.com:orgname/reponame.git。
在其他环境(碰巧都是Linux)中,我们希望使用令牌身份验证通过HTTPS访问存储库。对于这些环境,我们可以使用remote = https://github.com/orgname/reponame.git。
如何创建在两个环境中都能工作的工作区文件?我尝试像这样使用select调用:
new_git_repository(
...
remote = select({
"@bazel_tools//src/conditions:linux": "https://github.com/orgname/reponame.git",
"@bazel_tools//src/conditions:darwin": "git@github.com:orgname/reponame.git",
}),
)但是我得到了一个错误
ERROR: An error occurred during the fetch of repository 'myrepo':
got value of type 'select' for attribute 'remote' of new_git_repository rule 'myrepo'; select may not be used in repository rules如果new_git_repository有一个接受列表的remotes选项(类似于http_archive的urls选项),那就好了,但它没有。
发布于 2020-05-24 17:54:19
select()可以与构建规则(用于可配置属性)一起使用,但不能与存储库规则一起使用。我不认为有一个完全微不足道的方法来完成你所描述的。
我真的会尝试统一身份验证方法,以及如何跨主机寻址和访问存储库。
如果由于某些原因无法实现,您可以定义“两者”依赖关系:
new_git_repository(
name = "some_repo_mac",
remote = "git@github.com:orgname/reponame.git",
...
)
new_git_repository(
name = "some_repo_linux",
remote = "https://github.com/orgname/reponame.git",
...
)并在稍后阶段执行select (解析):
alias(
name = "some_ext_lib",
actual = select({
"@bazel_tools//src/conditions:linux_x86_64": "@some_repo_linux//:lib",
"@bazel_tools//src/conditions:darwin_x86_64": "@some_repo_mac//:lib",
}),
visibility = ["//visibility:public"],
)它将污染您的依赖关系图,但实际上不会检索到不需要的外部依赖关系。
您还需要别名实际的目标(每个目标一个),而不是可能通过外部设备提供和访问的多个目标。
您可以通过一个宏和一个小的自定义规则自动执行该过程,以生成基于alias的选择器……
或者,您可以继续编写一个自定义的git抓取规则,将其绑定到repository_ctx.os中。至少在构建的这个阶段,我没有立即意识到另一种访问主机操作系统信息的方法。
https://stackoverflow.com/questions/61947566
复制相似问题