代码之家  ›  专栏  ›  技术社区  ›  diegosasw

Gitlab CI上的dotnet包或nuget包

  •  0
  • diegosasw  · 技术社区  · 6 年前

    我正在使用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 IS 2.1.401)将非常感谢,因为我无法创建包含多个DLL的有效nuget包。

    更新 : 另一种选择是:

    nuget pack ./*NuGet/*.nuspec -Version $VERSION -OutputDirectory pepe -Prop Configuration=Release -NoDefaultExcludes -Verbosity detailed
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Igor Goyda    6 年前

    必须检查生成日志中的错误。也许有一些礼物。 我尝试创建包,并使用以下内容: 1。目录结构

    solution \
       interfaces
           bin
               debug
                   netcore2.1
                       interfaces.dll
                       common.dll
                       configuration.dll
               interfaces.nuspec
           interfaces.csproj
           IService.cs
       models
       configuration
    

    2。NUPEC含量:

    <?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>
    

    三。从我运行的Solution\Interfaces文件夹:

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

    一切都很好,nupkg包含所有的*.dll。 但是如果出了问题(例如路径),我就会出错。

        2
  •  0
  •   diegosasw    6 年前

    我在Gitlab成功地从CI/CD为我的dotnet核心应用程序构建了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 它包含了构建、运行generate 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.Contracts ToolBelt.Application.Model )