大型代码库中的 Bazel 构建可能既缓慢又复杂。本技能提供经过验证的模式,包括远程缓存、并行执行和自定义规则,可显著缩短构建时间。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“bazel-build-optimization”。 Help me set up Bazel for a Python project with external dependencies
预期结果:
Here is a basic WORKSPACE.bazel configuration for Python:
```python
workspace(name = "myproject")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_python",
sha256 = "...",
strip_prefix = "rules_python-0.27.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.27.0/rules_python-0.27.0.tar.gz",
)
load("@rules_python//python:repositories.bzl", "py_repositories")
py_repositories()
```
Then create a BUILD.bazel file with py_library and py_test rules.
正在使用“bazel-build-optimization”。 How do I configure remote execution?
预期结果:
Add this to your .bazelrc for remote execution:
```bash
build:remote-exec --remote_executor=grpcs://remote.example.com
build:remote-exec --remote_instance_name=projects/myproject/instances/default
build:remote-exec --jobs=500
```
Security tips:
- Use TLS for remote endpoints
- Authenticate with OAuth or certificates
- Start with read-only remote cache before enabling execution
安全审计
安全Static analysis detected 36 potential issues (external_commands, network, filesystem patterns), but all are false positives. The skill contains documentation and templates for Bazel build optimization with example commands and configuration snippets - no actual executable code with security implications. URLs are legitimate documentation links, filesystem references are standard Bazel cache paths, and cryptographic mentions are SHA256 checksums for download verification.
质量评分
你能构建什么
为 CI/CD 流水线设置远程缓存
配置 Bazel 远程缓存以在 CI 运行器之间共享构建产物,将构建时间从数小时缩短到数分钟。
优化大型 TypeScript 代码库
应用最佳实践来组织 BUILD 文件、启用并行执行和配置磁盘缓存。
为 Docker 创建自定义构建规则
编写自定义 Bazel 规则以作为构建图的一部分构建 Docker 镜像,并进行正确的依赖跟踪。
试试这些提示
Help me set up Bazel for a new monorepo. I need to configure WORKSPACE.bazel with rules for JavaScript and Python. What are the essential configurations?
I want to set up remote caching for our Bazel builds. We use Google Cloud. Show me the .bazelrc configuration needed and any security considerations.
Create a custom Bazel rule that builds a Docker image. The rule should accept a Dockerfile, base image, and layer files as inputs.
Our Bazel build takes 30 minutes. Help me profile it using bazel build --profile and identify the slowest actions. What optimizations should I try first?
最佳实践
- 使用细粒度目标而非大型 glob 模式,以获得更好的增量构建
- 尽早启用远程缓存 - 它能带来最大的性能提升
- 在 WORKSPACE 中固定依赖项版本以确保可重现的构建
避免
- 对 srcs 使用 glob(['**/*.python']) - 导致不必要的重新构建
- 跳过 WORKSPACE 设置 - 导致依赖项解析不一致
- 忽略构建警告 - 小问题会变成技术债务