gpt4 book ai didi

c# - “dotnet build”命令找不到 NuGet 包(它们存在)

转载 作者:太空狗 更新时间:2023-10-29 23:28:30 25 4
gpt4 key购买 nike

我正在尝试从我的 Jenkins 服务器上的命令行构建我的 .NET Core 2.1 应用程序。

它在我的本地计算机(Windows 10、Visual Studio 2017 Enterprise)上构建良好,如果我在运行 Jenkins 的服务器上手动构建它(手动 checkout Git 存储库并运行 dotnet build) .只有当我在 Docker 容器中通过 Jenkins 执行此操作时,它才会失败。

错误:/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5):错误 NETSDK1064:包 Microsoft.CodeQuality.Analyzers,版本 2.6。 2 没有找到。它可能在 NuGet 恢复后已被删除。否则,NuGet 还原可能仅部分完成,这可能是由于最大路径长度限制所致。 [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]

包存在于~/.nuget/packages/

我知道我的 Jenkinsfile 目前有点奇怪,但这与我试图让这一切正常工作有关。似乎 dotnet restore 工作正常,但 dotnet build 不知何故找不到这些文件。

我已经尝试指定包目录(通过环境变量,使用 dotnet restore --sourcesdotnet build --sources

我几乎附上了我目前能想到的任何东西,但如果我忘记了什么,请告诉我。我现在很迷茫。

我的 Jenkins 文件:

pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
}
}
environment {
HOME = '.'
NUGET_PACKAGES = './.nuget/packages/'
}
stages {
stage('pre-build') {
steps {
// logging tooling versions
sh 'dotnet --info'
sh 'find ~/.nuget/packages/microsoft.codequality.analyzers/ -type f'
sh 'dotnet nuget locals all --list'
sh 'dotnet restore'
// sh 'dotnet clean'
}
}
stage('build') {
steps {
// sh 'dotnet restore --force --no-cache'
// sh 'ls ./packages/'
// sh 'find ./packages/microsoft.codequality.analyzers/ -type f'
sh 'dotnet build --no-restore'
}
}
stage('test') {
steps {
sh 'no test project configured'
// sh 'dotnet test'
}
}
}
}

我的.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<CodeAnalysisRuleSet></CodeAnalysisRuleSet>
<Features>IOperation</Features>
</PropertyGroup>

</Project>

一些构建细节:

$ docker --version
Docker version 18.06.1-ce, build e68fc7a

docker 容器内部:

$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.1.403
Commit: 04e15494b6

Runtime Environment:
OS Name: debian
OS Version: 9
OS Platform: Linux
RID: debian.9-arm
Base Path: /usr/share/dotnet/sdk/2.1.403/

Host (useful for support):
Version: 2.1.5
Commit: 290303f510

.NET Core SDKs installed:
2.1.403 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

$ find ./.nuget/packages/microsoft.codequality.analyzers/ -type f
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/vb/Microsoft.CodeQuality.VisualBasic.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/vb/Microsoft.CodeQuality.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/cs/Microsoft.CodeQuality.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/cs/Microsoft.CodeQuality.CSharp.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/.signature.p7s
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/ThirdPartyNotices.rtf
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.CSharp.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.VisualBasic.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DesignRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DocumentationRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/PerformanceRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/UsageRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/ReliabilityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/MaintainabilityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/SecurityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/PerformanceRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/MaintainabilityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/UsageRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DocumentationRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/ReliabilityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/NamingRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesDisabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/SecurityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DesignRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/NamingRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.2.6.2.nupkg.sha512
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.2.6.2.nupkg
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.nuspec
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/tools/uninstall.ps1
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/tools/install.ps1

$ dotnet nuget locals all --list
info : http-cache: ./.local/share/NuGet/v3-cache
info : global-packages: ./.nuget/packages/
info : temp: /tmp/NuGetScratch
info : plugins-cache: ./.local/share/NuGet/plugins-cache

$ dotnet restore

Restoring packages for /var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj...

Restore completed in 13.5 sec for /var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj.

$ dotnet build --no-restore

Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1064: Package Microsoft.CodeQuality.Analyzers, version 2.6.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]

Build FAILED.

/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1064: Package Microsoft.CodeQuality.Analyzers, version 2.6.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]
0 Warning(s)
1 Error(s)

最佳答案

我通过指定 dotnet build 应该在哪里寻找 NuGet 包解决了这个问题。它在图像中看不到正确的位置(虽然我也不确定它看的是哪里...)

我已经使用 --packages 选项指定了位置。此选项在 dotnet restore 文档中列出,但未在 dotnet build 文档中列出,即使它在那里也可用(您特别需要它)。

您可以指定如下:

dotnet restore --packages <path>

dotnet build --packages <path>

如果您需要,它也可以与 --no-restore 选项一起使用。


我最终的 Jenkinsfile 如下所示:

pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
}
}
environment {
HOME = '.'
}
stages {
stage('pre-build') {
steps {
// logging tooling versions
sh 'dotnet --info'
sh 'dotnet nuget locals all --list'
sh 'dotnet clean'
}
}
stage('build') {
steps {
sh 'dotnet build --packages ./.nuget/packages'
}
}
stage('test') {
steps {
sh 'echo no test project configured'
// sh 'dotnet test'
}
}
}
}

关于c# - “dotnet build”命令找不到 NuGet 包(它们存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52864346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com