在GitLab CI中使用dotnet pack或nuget pack

8
我正在使用GitLab,需要创建一个.gitlab-ci.yml脚本来运行连续集成管道,以生成nuGet软件包的项目。但我无法找到可靠的文档来回答以下问题:我应该使用dotnet pack还是nuget pack?除非我的CI脚本下载它(我正在使用GitLab,所以我必须在.gitlab-ci.yml上添加一些内容来完成),否则nuget.exe不可用作为命令。我理解的是,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
3个回答

4

我现在可以通过GitLab的CI/CD成功地为我的.NET Core应用程序构建nuGet包。

需要考虑的事项:

  • 路径很重要:我的nuspec中的路径使用了Windows风格的\,我需要将它们改为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)。


太棒了!你愿意分享一个演示项目吗? - Seth Bergman
当然,这是我的一个开源软件包的演示项目 https://gitlab.com/sunnyatticsoftware/sasw-test-support - diegosasw

2

0
你必须检查构建日志以查找错误。也许有一些存在。 我尝试创建包并使用以下内容: 1. 目录结构
solution \
   interfaces
       bin
           debug
               netcore2.1
                   interfaces.dll
                   common.dll
                   configuration.dll
           interfaces.nuspec
       interfaces.csproj
       IService.cs
   models
   configuration

2. Nuspec 内容:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
    <metadata>
        <id>Interfaces</id>
        <version>1.0.0</version>
        <authors>Interfaces</authors>
        <owners>Interfaces</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Package Description</description>
    </metadata>
    <files>
        <file src="netstandard2.0\Common.dll" target="lib\netstandard2.0\Common.dll" />
        <file src="netstandard2.0\Configuration.dll" target="lib\netstandard2.0\Configuration.dll" />
        <file src="netstandard2.0\Interfaces.dll" target="lib\netstandard2.0\Interfaces.dll" />
        <file src="netstandard2.0\Models.dll" target="lib\netstandard2.0\Models.dll" />
    </files>
</package>

3. 我从 solution\interfaces 文件夹中运行:

dotnet pack interfaces.csproj /p:NuspecFile="bin\debug\interfaces.nuspec"

一切都很好,nupkg 包含所有的 *.dll。 但如果出现了问题(例如路径),我会收到错误信息。


但是,如果您有多个csproj文件怎么办?在dotnet pack中引用哪一个呢? 这就是为什么我更喜欢nuget pack而不是dotnet pack的原因,因为我有多个csproj文件,不知道应该选择哪一个。 我的做法类似于这样:nuget pack ./*NuGet/*.nuspec -Version $VERSION -OutputDirectory nupkgs -Prop Configuration=Release -NoDefaultExcludes -Verbosity detailed - diegosasw
1
在这种情况下,您可以创建一个元项目,只是为了创建NuGet包。这是某种解决方法。 - Igor Goyda
没错,这就是为什么我更喜欢在有 nuspec 文件时使用 nuget 命令的原因。因为 nuspec 文件“知道”解决方案中所有项目(所有 dll)的情况,而 dotnet pack 则需要像你建议的那样进行变通处理。但还是谢谢! - diegosasw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接