如何通过CICD流水线将ASP.NET Core应用部署到Azure Linux应用服务计划
适配Linux应用服务计划的Azure流水线yml修改方案
问题根因说明
- 使用
windows-latest构建时,默认生成的是适配Windows运行时的应用包,部署到Linux应用服务环境后架构不兼容,所以出现应用报错。 - 直接修改
vmImage为ubuntu-latest后报错,是因为Azure自动生成的默认yml中包含仅支持Windows的任务(如Windows PowerShell任务、针对Windows环境的部署参数)。
具体修改步骤
1. 统一流水线运行环境
将全局vmImage设置为ubuntu-latest,全程使用Linux环境构建打包,保证运行时架构匹配。
2. 替换Windows专属任务
移除所有PowerShell类型的任务,替换为Bash/script任务,同时修改.NET构建、发布命令适配Linux环境。
3. 调整部署任务参数
在Azure Web App部署任务中指定目标运行时为Linux,同时确认发布的包适配Linux运行时架构。
可用的完整yml示例
trigger: - main # 替换为你的实际触发分支 variables: buildConfiguration: 'Release' azureSubscription: '你的Azure服务连接名称' # 替换为你自己的服务连接名 webAppName: '你的Linux应用服务名称' # 替换为你的应用服务名 stages: - stage: Build displayName: Build stage jobs: - job: Build displayName: Build pool: vmImage: 'ubuntu-latest' steps: - task: UseDotNet@2 inputs: packageType: 'sdk' version: '5.x' # 匹配你的.NET 5版本 installationPath: $(Agent.ToolsDirectory)/dotnet - script: dotnet restore displayName: 'Restore NuGet packages' - script: dotnet build --configuration $(buildConfiguration) --no-restore displayName: 'Build project' - script: dotnet publish --configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory) --no-build --self-contained false --runtime linux-x64 displayName: 'Publish project for Linux runtime' - task: PublishBuildArtifacts@1 inputs: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: 'drop' publishLocation: 'Container' - stage: Deploy displayName: Deploy stage dependsOn: Build condition: succeeded() jobs: - deployment: Deploy displayName: Deploy to Linux App Service environment: 'production' pool: vmImage: 'ubuntu-latest' strategy: runOnce: deploy: steps: - task: DownloadBuildArtifacts@0 inputs: buildType: 'current' downloadType: 'single' artifactName: 'drop' downloadPath: '$(System.ArtifactsDirectory)' - task: AzureWebApp@1 inputs: azureSubscription: $(azureSubscription) appType: 'webAppLinux' # 必须指定为Linux应用服务类型 appName: $(webAppName) package: '$(System.ArtifactsDirectory)/drop/*.zip' runtimeStack: 'DOTNET|5.0' # 匹配你的.NET版本和运行时
额外检查项
- 确认你的Linux应用服务配置中,运行时堆栈已设置为
.NET 5,避免运行时版本不匹配。 - 如果应用有特殊的启动命令,可以在部署任务的
StartupCommand参数中指定,也可以在Azure门户的应用服务配置中手动设置。
内容的提问来源于stack exchange,提问作者Unknown
相关产品推荐
相关产品推荐

