我在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
)