.NET 8项目Azure DevOps Pipeline包发布步骤失败求助
问题排查:Azure DevOps Pipeline发布.NET 8项目失败(NETSDK1018错误)
问题描述
我有一个部署在Azure Function App上的.NET 8项目,使用Azure DevOps Pipeline作为CI/CD流程,此前运行完全正常。未修改代码,仅新增了Postman测试任务到Pipeline中,但Pipeline在包发布步骤就失败了(尚未执行到Postman测试环节),可确定问题与本次改动无关。
错误日志
Publish Package View raw log Starting: Publish Package ============================================================================== Task : .NET Core Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command Version : 2.238.1 Author : Microsoft Corporation Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli ============================================================================== Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version. /opt/hostedtoolcache/dotnet/dotnet publish /home/vsts/work/1/s/Functions/Functions.csproj --no-restore --configuration Release --output /home/vsts/work/1/App -p:version=1.0.1-DEVS-101_PostmanTest MSBuild version 17.9.8+b34f75857 for .NET Copying IronPdf files from /home/vsts/.nuget/packages/ironpdf.native.chrome.windows/2024.1.143/buildTransitive/netstandard2.0/../../runtimes to build directory for target framework net8.0 on Windows... /opt/hostedtoolcache/dotnet/sdk/8.0.204/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.GenerateAssemblyInfo.targets(226,5): error NETSDK1018: Invalid NuGet version string: '1.0.1-DEVS-101_PostmanTest'. [/home/vsts/work/1/s/Functions/Functions.csproj] ##[error]Error: The process '/opt/hostedtoolcache/dotnet/dotnet' failed with exit code 1 ##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting ##[error]Dotnet command failed with non-zero exit code on the following projects : [ '/home/vsts/work/1/s/Functions/Functions.csproj' ] Finishing: Publish Package
失败对应的Pipeline任务配置
- task: DotNetCoreCLI@2 name: Publish_Package displayName: Publish Package inputs: command: publish publishWebProjects: false projects: $(Project) arguments: --no-restore --configuration Release --output $(Pipeline.Workspace)/App -p:version=$(Build.BuildNumber) modifyOutputPath: false
排查方向与修复建议
- 核心问题定位:错误
NETSDK1018明确指出版本号1.0.1-DEVS-101_PostmanTest不符合NuGet规范。NuGet版本号的预发布标签部分仅允许使用字母、数字、连字符(-)和点(.),下划线(_)是非法字符。 - 问题根源:当前使用的
$(Build.BuildNumber)生成了包含下划线的版本号。虽然你仅新增了Postman测试任务,但可能触发了BuildNumber生成规则的变更(比如分支名包含下划线,或者BuildNumber格式被意外修改)。 - 修复方案:
- 调整BuildNumber格式:在Pipeline的“选项”页面修改构建编号格式,移除下划线,例如改为
1.0.1-DEVS-101.PostmanTest或1.0.1-DEVS101PostmanTest。 - 预处理版本号:在DotNetCoreCLI任务前添加PowerShell/Bash步骤,将
$(Build.BuildNumber)中的下划线替换为允许的字符,再传入-p:version参数。例如用PowerShell:$version = "$(Build.BuildNumber)" -replace '_', '.',然后在arguments中使用-p:version=$version。 - 检查变量与分支:确认是否有其他变量(如分支名称)被包含到BuildNumber中,且分支名带有下划线,针对性调整分支名或BuildNumber拼接逻辑。
- 调整BuildNumber格式:在Pipeline的“选项”页面修改构建编号格式,移除下划线,例如改为
- 验证步骤:修改后重新运行Pipeline,确认发布步骤能生成符合规范的版本号,完成发布流程。
内容的提问来源于stack exchange,提问作者Chidiebere Ugwu
相关产品推荐
相关产品推荐

