.net-core - gitlab CI 上的 dotnet pack 或 nuget pack

标签 .net-core nuget gitlab-ci

我正在使用 GitLab,需要创建一个 .gitlab-ci.yml 脚本来为生成 nuGet 包的项目运行持续集成管道。

我在寻找可靠的文档来回答这个问题时遇到严重问题:

我应该使用 dotnet pack 还是 nuget pack

nuget.exe 不能作为命令使用,除非我的 CI 脚本下载它(我使用的是 GitLab,所以我必须在 .gitlab-ci.yml 上添加一些内容才能执行此操作)。我的理解是 dotnet 隐式使用 nuget,因此不需要直接使用 nuget。

dotnet pack 命令的问题是我无法引用 nuspec 文件,它只是被忽略。

我尝试过

dotnet pack 'MyProject.Nuget/MyProject.Nuget.csproj' /p:NuspecFile='MyProject.Nuget/MyProject.NuGet.nuspec' /p:PackageVersion=$VERSION --output nupkgs

任何有关最新版本(dotnet --version 是 2.1.401)的正确方法的可靠文档将不胜感激,因为我无法创建包含多个 dll 的有效 nuGet 包。

更新: 另一种选择是:

nuget pack ./*NuGet/*.nuspec -Version $VERSION -OutputDirectory pepe -Prop Configuration=Release -NoDefaultExcludes -Verbosity detailed

最佳答案

我现在成功地从 GitLab 的 CI/CD 为我的 dotnet 核心应用程序构建了 nuGet 包。

需要考虑的事项:

  • 路径很重要:我的 nuspec 的路径带有 \窗口风格。我需要将它们更改为 linux 风格 / 。 确保您的文件 src 部分引用存在于指定路径的 dll 和 pdb,否则在创建 nuget 时会出现异常,因为它找不到任何内容。所以我的 nuspec 现在看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>ToolBelt.Application</id>
    <version>1.0.0</version>
    <authors>TUI</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Shared Contracts and Model for the Application layer</description>
    <projectUrl>https://gitlab.tuiwestern.eu/SalesDistribution/_common-packages/ToolBelt.Application</projectUrl>
  </metadata>
  <files>
    <file src="bin/*/netstandard2.0/ToolBelt.Application.Model.dll" target="lib/netstandard2.0" />
    <file src="bin/*/netstandard2.0/ToolBelt.Application.Model.pdb" target="lib/netstandard2.0" />
    <file src="bin/*/netstandard2.0/ToolBelt.Application.Contracts.dll" target="lib/netstandard2.0" />
    <file src="bin/*/netstandard2.0/ToolBelt.Application.Contracts.pdb" target="lib/netstandard2.0" />
  </files>
</package>
  • 该项目必须有 .gitlab-ci.yml其中包含构建、运行生成 nuget 并将其推送到 nuget 存储库(在我的情况下是在 Artifactory 中)的所有步骤。示例:
#Stages
stages:
  - ci
  - codequality
  - build
  - publish

#Global variables
variables:
  GIT_STRATEGY: $STRATEGY
  BUILD_NUMBER: $CI_PIPELINE_ID

#Jobs
ci:
  image: ocp-golden-images.artifactory.mycompany.eu/gitlab-runner-nuget-dotnet-core:latest
  stage: ci
  script:
    - export VERSION=$(echo $(date +"%Y.%-m%d").$CI_PIPELINE_ID)
    - export NUGET_PACKAGE_FOLDER=nupkgs
    - dotnet build --configuration Release
    - dotnet vstest ./*Tests/bin/Release/**/*Tests.dll
    - mkdir $NUGET_PACKAGE_FOLDER
    - nuget pack ./*NuGet/*.nuspec -Version $VERSION -OutputDirectory $NUGET_PACKAGE_FOLDER -Prop Configuration=Release -NoDefaultExcludes -Verbosity detailed
    - cd $NUGET_PACKAGE_FOLDER
    - dotnet nuget push *.$VERSION.nupkg -s https://artifactory.mycompany.eu/artifactory/api/nuget/MyRepo

因此,这些是构建具有如下结构的项目所需的所有命令:

myApp
-ToolBelt.Application.Nuget
--ToolBelt.Application.Nuget.nuspec
-ToolBelt.Application.Contracts
-ToolBelt.Application.Model
-ToolBelt.Application.Model.UnitTests

其中包含 nuspec 的项目(例如: ToolBelt.Application.Nuget )必须依赖于需要包含在包中的每个项目(例如: ToolBelt.Application.ContractsToolBelt.Application.Model )

关于.net-core - gitlab CI 上的 dotnet pack 或 nuget pack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271134/

相关文章:

.net-core - 如何从命令行运行 .NET Core 控制台应用程序

javascript - 如何将 RTF 字符串转换为 Markdown 字符串(以及转换回来)(C# .NET Core 或 JS)

c# - 由于 commandTimeout 不够健壮,如何使用 Dapper 在 C# 中获得可靠的阻塞/缓慢数据库操作超时?

svg - 是否可以通过shields.io 徽章从nuget.org 获取下载计数?

Gitlab Runner 作业失败

Gitlab-ci:如何在工件过期时禁用手动步骤?

c# - WebsphereMQ 与 .Net Core 2.2

visual-studio-2012 - 如何避免 TFS 和 nuget 升级错误

compiler-errors - 如何解决 “The type or namespace name ' Didstopia'找不到(您是否缺少using指令或程序集引用?)”

gitlab - 生成 gitlab ci 工件作为 native 文件类型