gitlab CI 上的 dotnet pack 或 nuget pack

ibe*_*dev 6 nuget gitlab-ci .net-core

我正在使用 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
Run Code Online (Sandbox Code Playgroud)

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

更新:替代方案是:

nuget pack ./*NuGet/*.nuspec -Version $VERSION -OutputDirectory pepe -Prop Configuration=Release -NoDefaultExcludes -Verbosity detailed
Run Code Online (Sandbox Code Playgroud)

ibe*_*dev 5

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

需要考虑的事项:

  • 路径很重要:我的 nuspec 有\Windows 风格的路径。我需要将它们更改为 linux-style /。确保您的文件 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>
Run Code Online (Sandbox Code Playgroud)
  • 该项目必须包含.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
Run Code Online (Sandbox Code Playgroud)

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

myApp
-ToolBelt.Application.Nuget
--ToolBelt.Application.Nuget.nuspec
-ToolBelt.Application.Contracts
-ToolBelt.Application.Model
-ToolBelt.Application.Model.UnitTests
Run Code Online (Sandbox Code Playgroud)

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