# 正在构建和测试 Rust

了解如何创建持续集成 (CI) 工作流来构建和测试 Rust 项目。

> \[!NOTE]
> GitHub Enterprise Server 目前不支持 GitHub 托管的运行器。

## 简介

本指南介绍如何构建、测试和发布 Rust 包。

GitHub 托管的运行器具有预装了软件的工具缓存，包括 Rust 的依赖项。 有关最新软件和预安装版本的 Rust 的完整列表，请参阅 [GitHub 托管的运行程序](/zh/enterprise-server@3.16/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software)。

## 先决条件

您应该已经熟悉 YAML 语法及其如何与 GitHub Actions 结合使用。 有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/enterprise-server@3.16/actions/using-workflows/workflow-syntax-for-github-actions)”。

建议你对 Rust 语言有基本的了解。 有关详细信息，请参阅 [Rust 入门](https://www.rust-lang.org/learn)。

## 使用 Rust 工作流模板

若要快速开始使用，请将工作流模板添加到存储库的 `.github/workflows` 目录。

GitHub 提供了 Rust 工作流模板，应该适用于大多数基本的 Rust 项目。 本指南的后续部分提供了如何自定义此工作流模板的示例。

1. 在 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”\*\*\*\*。

   ![“github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。](/assets/images/help/repository/actions-tab-global-nav-update.png)1. 如果存储库中已有工作流，请单击“新建工作流”。

2. “选择工作流”页面显示一系列推荐的工作流模板。 搜索“Rust”。

3. 单击“持续集成”\*\*\*\* 以筛选工作流选择。

4. 在“Rust - by GitHub Actions”工作流上，单击“配置”。\*\*\*\*

   ![“选择工作流”页的屏幕截图。 “Rust”工作流上的“配置”按钮以橙色轮廓突出显示。](/assets/images/help/actions/starter-workflow-rust.png)
   如果未找到“Rust - by GitHub Actions”工作流模板，请将以下工作流代码复制到仓库的 `rust.yml` 目录中名为 `.github/workflows` 的新文件。

   ```yaml copy
   name: Rust

   on:
     push:
       branches: [ "main" ]
     pull_request:
       branches: [ "main" ]

   env:
     CARGO_TERM_COLOR: never

   jobs:
     build:

       runs-on: ubuntu-latest

       steps:
       - uses: actions/checkout@v6
       - name: Build
         run: cargo build --verbose
       - name: Run tests
         run: cargo test --verbose
   ```

5. 根据需要编辑工作流。 例如更改 Rust 版本。

6. 单击“提交更改”。

## 指定 Rust 版本

GitHub 托管的运行器包含最新版本的 Rust 工具链。 可以使用 rustup 报告运行器上安装的版本、替代该版本以及安装不同的工具链。 有关详细信息，请参阅 [rustup 手册](https://rust-lang.github.io/rustup/)。

此示例演示了可用于设置运行器环境以使用 Rust 的夜间构建版本并报告此版本的步骤。

```yaml copy
      - name: Temporarily modify the rust toolchain version
        run: rustup override set nightly
      - name: Output rust version for educational purposes
        run: rustup --version
```

### 缓存依赖项

可以使用缓存操作来缓存和还原依赖项。 此示例假定仓库包含一个 `Cargo.lock` 文件。

```yaml copy
      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
```

如果有自定义要求或需要更精细的缓存控件，则应浏览[`cache`操作](https://github.com/marketplace/actions/cache)的其他配置选项。 有关详细信息，请参阅“[依赖项缓存参考](/zh/enterprise-server@3.16/actions/using-workflows/caching-dependencies-to-speed-up-workflows)”。

## 构建和测试代码

您可以使用与本地相同的命令来构建和测试代码。 此示例工作流演示如何在作业中使用 `cargo build` 和 `cargo test`：

```yaml copy
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        BUILD_TARGET: [release] # refers to a cargo profile
    outputs:
      release_built: ${{ steps.set-output.outputs.release_built }}
    steps:
      - uses: actions/checkout@v6
      - name: Build binaries in "${{ matrix.BUILD_TARGET }}" mode
        run: cargo build --profile ${{ matrix.BUILD_TARGET }}
      - name: Run tests in "${{ matrix.BUILD_TARGET }}" mode
        run: cargo test --profile ${{ matrix.BUILD_TARGET }}
```

此示例中使用的 `release` 关键字表示一个货物配置文件。 可以使用在 [](https://doc.rust-lang.org/cargo/reference/profiles.html) 文件中定义的任何`Cargo.toml`。

## 将包或库发布到 crates.io

设置工作流以构建和测试代码后，可以使用机密以登录 [crates.io](https://crates.io/) 并发布包。

```yaml copy
      - name: Login into crates.io
        run: cargo login ${{ secrets.CRATES_IO }}
      - name: Build binaries in "release" mode
        run: cargo build -r
      - name: "Package for crates.io"
        run: cargo package # publishes a package as a tarball
      - name: "Publish to crates.io"
        run: cargo publish # publishes your crate as a library that can be added as a dependency
```

如果在构建和打包 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)。

## 将工作流数据打包为构件

工作流完成后，就可以上传产生的项目进行分析或在另一个工作流中使用。 可以将这些示例步骤添加到工作流，以上传应用程序供另一个工作流使用。

```yaml copy
      - name: Upload release artifact
        uses: actions/upload-artifact@v3
        with:
          name: <my-app>
          path: target/${{ matrix.BUILD_TARGET }}/<my-app>
```

若要在不同的作业中使用上传的项目，请确保工作流对仓库具有适当的权限，请参阅 [在工作流中使用 GITHUB\_TOKEN 进行身份验证](/zh/enterprise-server@3.16/actions/security-for-github-actions/security-guides/automatic-token-authentication)。 可以使用这些示例步骤下载在上一工作流中创建的应用，并将其发布到 GitHub 上。

```yaml copy
      - uses: actions/checkout@v6
      - name: Download release artifact
        uses: actions/download-artifact@v3
        with:
          name: <my-app>
          path: ./<my-app>
      - name: Publish built binary to GitHub releases
      - run: |
          gh release create --generate-notes ./<my-app>/<my-project>#<my-app>
```