Skip to main content

Azure DevOps Pipeline 如何支持.Net 8项目

分类:  HowTo 标签:  #DevOps #.Net 发布于: 2025-02-16 18:49:11

今天正式发布了.Net 8,近期做了一些.Net 8的测试,对这个LTS版本特别满意,决定将自己的项目迁移到.Net8, 项目很小,只需要更改项目文件中的<TargetFramework>net8.0</TargetFramework>, 重新编译打包就可以完成升级了。

更改完项目之后,本地打包测试,运行正常,然后决定正式发布,将develop分支mergerelease分支,然后直接使用git pushAzure Devops, 正式发布,最后只需要静静等待就可以了。

等了一会儿收到了Azure DevOps的邮件:



收到了编译失败的邮件,登录到Azure DevOps查看编译详细,发现:

Starting: DotNetCoreCLI
==============================================================================
Task : .NET Core
Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version : 2.221.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
C:\Windows\system32\chcp.com 65001
Active code page: 65001
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.
"C:\Program Files\dotnet\dotnet.exe" restore D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj --configfile D:\a\1\Nuget\tempNuGet_107.config --verbosity Detailed
Build started 11/15/2023 6:26:26 AM.
0>Process = "C:\Program Files\dotnet\dotnet.exe"
MSBuild executable path = "C:\Program Files\dotnet\sdk\7.0.403\MSBuild.dll"
Command line arguments = "C:\Program Files\dotnet\sdk\7.0.403\MSBuild.dll -maxcpucount -verbosity:m -nologo -target:Restore -property:RestoreConfigFile=D:\a\1\Nuget\tempNuGet_107.config -verbosity:detailed D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,C:\Program Files\dotnet\sdk\7.0.403\dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,C:\Program Files\dotnet\sdk\7.0.403\dotnet.dll"
Current directory = "D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin"
MSBuild version = "17.7.3+4fca21998"

发现用于编译的命令仍然在默认使用.Net 7, 最终报错:

Build FAILED.

"D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj" (Restore target) (1) ->
"D:\a\1\s\AzureDeveloper.OrchardCore.Modules.AzureArticle\AzureDeveloper.OrchardCore.Modules.Articles.csproj" (_GenerateRestoreGraphProjectEntry target) (2:3) ->
(_CheckForUnsupportedNETCoreVersion target) ->
C:\Program Files\dotnet\sdk\7.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(160,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 8.0. Either target .NET 7.0 or lower, or use a version of the .NET SDK that supports .NET 8.0. Download the .NET SDK from https://aka.ms/dotnet/download [D:\a\1\s\AzureDeveloper.OrchardCore.Modules.AzureArticle\AzureDeveloper.OrchardCore.Modules.Articles.csproj]


"D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj" (Restore target) (1) ->
"D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj" (_GenerateRestoreGraphProjectEntry target) (1:5) ->
C:\Program Files\dotnet\sdk\7.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(160,5): error NETSDK1045: The current .NET SDK does not support targeting .NET 8.0. Either target .NET 7.0 or lower, or use a version of the .NET SDK that supports .NET 8.0. Download the .NET SDK from https://aka.ms/dotnet/download [D:\a\1\s\AzureDeveloper.OrchardCore.Themes.FirstBegin\AzureDeveloper.OrchardCore.Themes.FirstBegin.csproj]

0 Warning(s)
2 Error(s)

Time Elapsed 00:00:20.98
##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
##[error]Packages failed to restore
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
Finishing: DotNetCoreCLI

这个报错说明项目的目标是.Net 8, 但是实际使用的编译的SDK仍然是.Net 7, 为了缓解这个问题,查询文档发现可以使用task UseDotNet@2, 该Task的官方文档可以参考:https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/use-dotnet-v2?view=azure-pipelines&viewFallbackFrom=azure-devops

注意
Task UseDotNet@2 需要联合Task DotNetCoreCLI@2一起使用。

最后的实例如下:

    - job: build_AzureDeveloper_OrchardCore_Web
displayName: Build Project AzureDeveloper OrchardCore Web
steps:

- task: UseDotNet@2
displayName: Install .Net SDK 8
inputs:
packageType: sdk
version: 8.0.100
installationPath: $(Agent.ToolsDirectory)/dotnet

- task: DotNetCoreCLI@2
inputs:
command: build
projects: 'AzureDeveloper.OrchardCore.Web/AzureDeveloper.OrchardCore.Web.csproj'
arguments: --configuration $(buildConfiguration)
displayName: Web> dotnet build $(buildConfiguration)

完整的DevOpS 配置如下, 该实例监视release分支,只要release分支有推送,即会自动开始进行部署,没有演示需要rollback的情况,后面再找时间演示如何rollback到某个特定的版本。

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
branches:
include:
- release
paths:
include:
- AzureDeveloper.OrchardCore.Web

variables:
- group: webdeploy_variables
- name: buildConfiguration
value: 'Release'




stages:
- stage: restore_whole_solution
displayName: Restore whole solution
pool:
vmImage: windows-latest
jobs:

- job: restore_azuredeveloper_orchardcore_web
displayName: Restore Project AzureDeveloper OrchardCore Web
workspace:
clean: all
steps:
- task: UseDotNet@2
displayName: Install .Net SDK 8.0
inputs:
packageType: sdk
version: 8.0.100
installationPath: $(Agent.ToolsDirectory)/dotnet
- task: DotNetCoreCLI@2
displayName: Restore Project AzureDeveloper.OrchardCore.Web
inputs:
command: restore
projects: 'AzureDeveloper.OrchardCore.Web/AzureDeveloper.OrchardCore.Web.csproj'

- stage: build_whole_solution
displayName: Build Whole Solution
pool:
vmImage: windows-latest
jobs:


- job: build_AzureDeveloper_OrchardCore_Web
displayName: Build Project AzureDeveloper OrchardCore Web
steps:

- task: UseDotNet@2
displayName: Install .Net SDK 8
inputs:
packageType: sdk
version: 8.0.100
installationPath: $(Agent.ToolsDirectory)/dotnet

- task: DotNetCoreCLI@2
inputs:
command: build
projects: 'AzureDeveloper.OrchardCore.Web/AzureDeveloper.OrchardCore.Web.csproj'
arguments: --configuration $(buildConfiguration)
displayName: Web> dotnet build $(buildConfiguration)

- job: public_web_project_to_zip
displayName: Publish Web Project To a Zip File
dependsOn: build_AzureDeveloper_OrchardCore_Web
steps:

- task: UseDotNet@2
displayName: Install .Net SDK 8
inputs:
packageType: sdk
version: 8.0.100
installationPath: $(Agent.ToolsDirectory)/dotnet

- task: DotNetCoreCLI@2
inputs:
command: publish
projects: 'AzureDeveloper.OrchardCore.Web/AzureDeveloper.OrchardCore.Web.csproj'
arguments: --configuration $(buildConfiguration)
publishWebProjects: true
displayName: dotnet publish --configuration $(buildConfiguration)

- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(System.DefaultWorkingDirectory)/AzureDeveloper.OrchardCore.Web/bin/Release/net8.0/'
artifactType: 'pipeline'
artifactName: 'AzureDeveloper.OrchardCore.Web'


- stage: deploy_web_project_to_production
displayName: Deploy Web Project to Production
pool:
vmImage: windows-latest
jobs:
- deployment: deploy_web_project
workspace:
clean: true
environment:
name: Productions
resourceType: VirtualMachine
strategy:
rolling:
deploy:
steps:
- script: echo $(System.DefaultWorkingDirectory)
- task: IISWebAppDeploymentOnMachineGroup@0
inputs:
WebSiteName: 'www.azuredeveloper.cn'
Package: '$(System.DefaultWorkingDirectory)\..\AzureDeveloper.OrchardCore.Web\*.zip'
TakeAppOfflineFlag: true