{"meta":{"title":"正在构建和测试 Rust","intro":"了解如何创建持续集成 (CI) 工作流来构建和测试 Rust 项目。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/enterprise-server@3.16/actions","title":"GitHub Actions"},{"href":"/zh/enterprise-server@3.16/actions/tutorials","title":"教程"},{"href":"/zh/enterprise-server@3.16/actions/tutorials/build-and-test-code","title":"构建和测试代码"},{"href":"/zh/enterprise-server@3.16/actions/tutorials/build-and-test-code/rust","title":"Rust"}],"documentType":"article"},"body":"# 正在构建和测试 Rust\n\n了解如何创建持续集成 (CI) 工作流来构建和测试 Rust 项目。\n\n> \\[!NOTE]\n> GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。\n\n## 简介\n\n本指南介绍如何构建、测试和发布 Rust 包。\n\nGitHub 托管的运行器具有预装了软件的工具缓存，包括 Rust 的依赖项。 有关最新软件和预安装版本的 Rust 的完整列表，请参阅 [GitHub 托管的运行程序](/zh/enterprise-server@3.16/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software)。\n\n## 先决条件\n\n您应该已经熟悉 YAML 语法及其如何与 GitHub Actions 结合使用。 有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/enterprise-server@3.16/actions/using-workflows/workflow-syntax-for-github-actions)”。\n\n建议你对 Rust 语言有基本的了解。 有关详细信息，请参阅 [Rust 入门](https://www.rust-lang.org/learn)。\n\n## 使用 Rust 工作流模板\n\n若要快速开始使用，请将工作流模板添加到存储库的 `.github/workflows` 目录。\n\nGitHub 提供了 Rust 工作流模板，应该适用于大多数基本的 Rust 项目。 本指南的后续部分提供了如何自定义此工作流模板的示例。\n\n1. 在 GitHub 上，导航到存储库的主页面。1. 在仓库名称下，单击“<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-play\" aria-label=\"play\" role=\"img\"><path d=\"M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z\"></path></svg> Actions”\\*\\*\\*\\*。\n\n   ![“github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。](/assets/images/help/repository/actions-tab-global-nav-update.png)1. 如果存储库中已有工作流，请单击“新建工作流”。\n\n2. “选择工作流”页面显示一系列推荐的工作流模板。 搜索“Rust”。\n\n3. 单击“持续集成”\\*\\*\\*\\* 以筛选工作流选择。\n\n4. 在“Rust - by GitHub Actions”工作流上，单击“配置”。\\*\\*\\*\\*\n\n   ![“选择工作流”页的屏幕截图。 “Rust”工作流上的“配置”按钮以橙色轮廓突出显示。](/assets/images/help/actions/starter-workflow-rust.png)\n   如果未找到“Rust - by GitHub Actions”工作流模板，请将以下工作流代码复制到仓库的 `rust.yml` 目录中名为 `.github/workflows` 的新文件。\n\n   ```yaml copy\n   name: Rust\n\n   on:\n     push:\n       branches: [ \"main\" ]\n     pull_request:\n       branches: [ \"main\" ]\n\n   env:\n     CARGO_TERM_COLOR: never\n\n   jobs:\n     build:\n\n       runs-on: ubuntu-latest\n\n       steps:\n       - uses: actions/checkout@v6\n       - name: Build\n         run: cargo build --verbose\n       - name: Run tests\n         run: cargo test --verbose\n   ```\n\n5. 根据需要编辑工作流。 例如更改 Rust 版本。\n\n6. 单击“提交更改”。\n\n## 指定 Rust 版本\n\nGitHub 托管的运行器包含最新版本的 Rust 工具链。 可以使用 rustup 报告运行器上安装的版本、替代该版本以及安装不同的工具链。 有关详细信息，请参阅 [rustup 手册](https://rust-lang.github.io/rustup/)。\n\n此示例演示了可用于设置运行器环境以使用 Rust 的夜间构建版本并报告此版本的步骤。\n\n```yaml copy\n      - name: Temporarily modify the rust toolchain version\n        run: rustup override set nightly\n      - name: Output rust version for educational purposes\n        run: rustup --version\n```\n\n### 缓存依赖项\n\n可以使用缓存操作来缓存和还原依赖项。 此示例假定仓库包含一个 `Cargo.lock` 文件。\n\n```yaml copy\n      - name: Cache\n        uses: actions/cache@v4\n        with:\n          path: |\n            ~/.cargo/registry\n            ~/.cargo/git\n            target\n          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}\n```\n\n如果有自定义要求或需要更精细的缓存控件，则应浏览[`cache`操作](https://github.com/marketplace/actions/cache)的其他配置选项。 有关详细信息，请参阅“[依赖项缓存参考](/zh/enterprise-server@3.16/actions/using-workflows/caching-dependencies-to-speed-up-workflows)”。\n\n## 构建和测试代码\n\n您可以使用与本地相同的命令来构建和测试代码。 此示例工作流演示如何在作业中使用 `cargo build` 和 `cargo test`：\n\n```yaml copy\njobs:\n  build:\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        BUILD_TARGET: [release] # refers to a cargo profile\n    outputs:\n      release_built: ${{ steps.set-output.outputs.release_built }}\n    steps:\n      - uses: actions/checkout@v6\n      - name: Build binaries in \"${{ matrix.BUILD_TARGET }}\" mode\n        run: cargo build --profile ${{ matrix.BUILD_TARGET }}\n      - name: Run tests in \"${{ matrix.BUILD_TARGET }}\" mode\n        run: cargo test --profile ${{ matrix.BUILD_TARGET }}\n```\n\n此示例中使用的 `release` 关键字表示一个货物配置文件。 可以使用在 [](https://doc.rust-lang.org/cargo/reference/profiles.html) 文件中定义的任何`Cargo.toml`。\n\n## 将包或库发布到 crates.io\n\n设置工作流以构建和测试代码后，可以使用机密以登录 [crates.io](https://crates.io/) 并发布包。\n\n```yaml copy\n      - name: Login into crates.io\n        run: cargo login ${{ secrets.CRATES_IO }}\n      - name: Build binaries in \"release\" mode\n        run: cargo build -r\n      - name: \"Package for crates.io\"\n        run: cargo package # publishes a package as a tarball\n      - name: \"Publish to crates.io\"\n        run: cargo publish # publishes your crate as a library that can be added as a dependency\n```\n\n如果在构建和打包 crate 时出现任何错误，请检查清单 `Cargo.toml` 文件中的元数据，请参阅“清单格式”。[](https://doc.rust-lang.org/cargo/reference/manifest.html) 还应检查 `Cargo.lock` 文件，请参阅 [Cargo.toml 与 Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html)。\n\n## 将工作流数据打包为构件\n\n工作流完成后，就可以上传产生的项目进行分析或在另一个工作流中使用。 可以将这些示例步骤添加到工作流，以上传应用程序供另一个工作流使用。\n\n```yaml copy\n      - name: Upload release artifact\n        uses: actions/upload-artifact@v3\n        with:\n          name: <my-app>\n          path: target/${{ matrix.BUILD_TARGET }}/<my-app>\n```\n\n若要在不同的作业中使用上传的项目，请确保工作流对仓库具有适当的权限，请参阅 [在工作流中使用 GITHUB\\_TOKEN 进行身份验证](/zh/enterprise-server@3.16/actions/security-for-github-actions/security-guides/automatic-token-authentication)。 可以使用这些示例步骤下载在上一工作流中创建的应用，并将其发布到 GitHub 上。\n\n```yaml copy\n      - uses: actions/checkout@v6\n      - name: Download release artifact\n        uses: actions/download-artifact@v3\n        with:\n          name: <my-app>\n          path: ./<my-app>\n      - name: Publish built binary to GitHub releases\n      - run: |\n          gh release create --generate-notes ./<my-app>/<my-project>#<my-app>\n```"}