{"meta":{"title":"构建和测试 .NET","intro":"了解如何创建持续集成 (CI) 工作流来构建和测试 .NET 项目。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/enterprise-cloud@latest/actions","title":"GitHub Actions"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials","title":"教程"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials/build-and-test-code","title":"构建和测试代码"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials/build-and-test-code/net","title":".NET"}],"documentType":"article"},"body":"# 构建和测试 .NET\n\n了解如何创建持续集成 (CI) 工作流来构建和测试 .NET 项目。\n\n## 简介\n\n本指南介绍如何构建、测试和发布 .NET 包。\n\nGitHub托管运行器具有一个工具缓存，其中预装了软件，包括 .NET Core SDK。 有关最新软件以及预安装的 .NET Core SDK 版本的完整列表，请参阅 [GitHub 托管运行器上安装的软件](/zh/enterprise-cloud@latest/actions/concepts/runners/github-hosted-runners)。\n\n## 先决条件\n\n你应该已经熟悉 YAML 语法，以及如何将其与 GitHub Actions 一起使用。 有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/enterprise-cloud@latest/actions/reference/workflows-and-actions/workflow-syntax)”。\n\n建议您对 .NET Core SDK 有个基本的了解。 有关详细信息，请参阅 [.NET 入门](https://dotnet.microsoft.com/learn)。\n\n## 使用 .NET 工作流模板\n\n若要快速开始使用，请将工作流模板添加到存储库的 `.github/workflows` 目录。\n\nGitHub提供适用于大多数.NET项目的.NET的工作流模板。 本指南的后续部分提供了如何自定义此工作流模板的示例。\n\n1. 在 GitHub 上，导航到存储库的主页面。\n\n2. 在仓库名称下，单击“<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)\n\n3. 如果存储库中已有工作流，请单击“新建工作流”。\n\n4. “选择工作流”页面显示一系列推荐的工作流模板。 搜索“dotnet”。\n\n5. 在“.NET”工作流上，单击“**配置**”。\n\n6. 根据需要编辑工作流。 例如更改 .NET 版本。\n\n7. 单击“提交更改”。\n\n   工作流 `dotnet.yml` 文件将添加到 `.github/workflows` 存储库的目录中。\n\n## 指定 .NET 版本\n\n若要在 GitHub 托管的运行器上使用预安装的 .NET Core SDK 版本，请使用 `setup-dotnet` 操作。 此操作从每个运行器上的工具缓存中查找特定版本的 .NET，并将必要的二进制文件添加到 `PATH`。 这些更改将在任务的剩余部分中持续有效。\n\n`setup-dotnet` 操作是将 .NET 与 GitHub Actions 配合使用的推荐方式，因为它可确保在不同运行器和不同版本的 .NET 之间具有一致的行为。 如果使用自托管运行器，则必须安装 .NET 并将其添加到 `PATH`。 有关详细信息，请参阅 [`setup-dotnet`](https://github.com/marketplace/actions/setup-net-core-sdk) 操作。\n\n### 使用多个 .NET 版本\n\n```yaml\nname: dotnet package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        dotnet-version: [ '3.1.x', '6.0.x' ]\n\n    steps:\n      - uses: actions/checkout@v6\n      - name: Setup dotnet ${{ matrix.dotnet-version }}\n        uses: actions/setup-dotnet@v4\n        with:\n          dotnet-version: ${{ matrix.dotnet-version }}\n      # You can test your matrix by printing the current dotnet version\n      - name: Display dotnet version\n        run: dotnet --version\n```\n\n### 使用特定的 .NET 版本\n\n可以将作业配置为使用 .NET 的特定版本，例如 `6.0.22`。 或者，您也可以使用语义版本语法来获得最新的次要版本。 此示例使用 .NET 6 最新的次要版本。\n\n```yaml\n    - name: Setup .NET 6.x\n      uses: actions/setup-dotnet@v4\n      with:\n        # Semantic version range syntax or exact version of a dotnet version\n        dotnet-version: '6.x'\n```\n\n## 安装依赖\n\nGitHub-托管运行程序已安装 NuGet 包管理器。 在构建和测试代码之前，您可以使用 dotnet CLI 从 NuGet 软件包注册表安装依赖项。 例如，下面的 YAML 会安装 `Newtonsoft` 包。\n\n```yaml\nsteps:\n- uses: actions/checkout@v6\n- name: Setup dotnet\n  uses: actions/setup-dotnet@v4\n  with:\n    dotnet-version: '6.0.x'\n- name: Install dependencies\n  run: dotnet add package Newtonsoft.Json --version 12.0.1\n```\n\n### 缓存依赖项\n\n可以使用可选的 `cache` 输入来为将来的工作流缓存 NuGet 依赖项。 例如，下面的 YAML 缓存 NuGet `global-packages` 文件夹，然后安装 `Newtonsoft` 包。 第二个可选输入 `cache-dependency-path` 可用于指定依赖项文件的路径：`packages.lock.json`。\n\n有关详细信息，请参阅“[依赖项缓存参考](/zh/enterprise-cloud@latest/actions/reference/workflows-and-actions/dependency-caching)”。\n\n```yaml\nsteps:\n- uses: actions/checkout@v6\n- name: Setup dotnet\n  uses: actions/setup-dotnet@v4\n  with:\n    dotnet-version: '6.x'\n    cache: true\n- name: Install dependencies\n  run: dotnet add package Newtonsoft.Json --version 12.0.1\n```\n\n> \\[!NOTE]\n> 根据依赖项的数量，使用依赖项缓存可能会更快。 有很多大型依赖项的项目应该能看到性能明显提升，因为下载所需的时间会缩短。 依赖项较少的项目可能看不到明显的性提升，甚至可能由于 NuGet 安装缓存依赖项的方式而看到性能略有下降。 性能因项目而异。\n\n## 构建和测试代码\n\n您可以使用与本地相同的命令来构建和测试代码。 此示例演示如何在作业中使用 `dotnet build` 和 `dotnet test`：\n\n```yaml\nsteps:\n- uses: actions/checkout@v6\n- name: Setup dotnet\n  uses: actions/setup-dotnet@v4\n  with:\n    dotnet-version: '6.0.x'\n- name: Install dependencies\n  run: dotnet restore\n- name: Build\n  run: dotnet build --no-restore\n- name: Test with the dotnet CLI\n  run: dotnet test --no-build\n```\n\n## 将工作流数据打包为构件\n\n工作流程完成后，您可以上传产生的项目进行分析。 例如，您可能需要保存日志文件、核心转储、测试结果或屏幕截图。 以下示例演示如何使用 `upload-artifact` 操作上传测试结果。\n\n有关详细信息，请参阅“[使用工作流工件存储和共享数据](/zh/enterprise-cloud@latest/actions/tutorials/store-and-share-data)”。\n\n```yaml\nname: dotnet package\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        dotnet-version: [ '3.1.x', '6.0.x' ]\n\n      steps:\n        - uses: actions/checkout@v6\n        - name: Setup dotnet\n          uses: actions/setup-dotnet@v4\n          with:\n            dotnet-version: ${{ matrix.dotnet-version }}\n        - name: Install dependencies\n          run: dotnet restore\n        - name: Test with dotnet\n          run: dotnet test --no-restore --logger trx --results-directory \"TestResults-${{ matrix.dotnet-version }}\"\n        - name: Upload dotnet test results\n          uses: actions/upload-artifact@v4\n          with:\n            name: dotnet-results-${{ matrix.dotnet-version }}\n            path: TestResults-${{ matrix.dotnet-version }}\n          # Use always() to always run this step to publish test results when there are test failures\n          if: ${{ always() }}\n```\n\n## 发布到包注册表\n\n您可以配置工作流程在 CI 测试通过后将 .NET 包发布到包注册表。 您可以使用仓库机密来存储发布二进制文件所需的任何令牌或凭据。 以下示例创建包并将其发布到 GitHub Packages 使用 `dotnet core cli`。\n\n```yaml\nname: Upload dotnet package\n\non:\n  release:\n    types: [created]\n\njobs:\n  deploy:\n    runs-on: ubuntu-latest\n    permissions:\n      packages: write\n      contents: read\n    steps:\n      - uses: actions/checkout@v6\n      - uses: actions/setup-dotnet@v4\n        with:\n          dotnet-version: '6.0.x' # SDK Version to use.\n          source-url: https://nuget.pkg.github.com/<owner>/index.json\n        env:\n          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}\n      - run: dotnet build --configuration Release <my project>\n      - name: Create the package\n        run: dotnet pack --configuration Release <my project>\n      - name: Publish the package to GPR\n        run: dotnet nuget push <my project>/bin/Release/*.nupkg\n```"}