使用 .Net Core 的 Azure Pipelines - 为每个环境转换 web.config

问题描述 投票:0回答:2

我们在使用 .Net Core 迁移到 Azure Devops 以部署到我们的本地 Windows 服务器时遇到了问题。这可能是由于缺乏对平台的了解,但尽管如此......

我们试图解决的问题是,当遵循“一次构建,到处部署”结构时,我们如何转换 web.config 文件以将“ASPNETCORE_ENVIRONMENT”环境变量设置为正在部署的正确环境。目前我们的应用程序需要该变量集,因为我们的 appsettings 分为 2 个文件:appSettings.json 和 appSettings.Env.json,其中 Env 是我们要部署到的当前环境(Dev 或 Prod)。

例如: Azure Pipelines 构建我们的项目,输出工件中生成的 web.config 将 ASPNETCORE_ENVIRONMENT 变量设置为“开发”。这很棒,因为我们随后将该工件部署到我们的开发环境中。但是当我们将其发布到产品阶段时会发生什么?该 web.config 仍然将 ASPNETCORE_ENVIRONMENT 变量设置为“开发”。

转换该配置的正确(2021)方法是什么?我是否应该在 web.config 中使用该变量并更改与 appSettings 文件交互的方式?

我已经阅读了很多关于类似问题的文章和问题,但与 ASPNETCORE_ENVIRONMENT 环境变量相关的文章和问题并不多。有人说使用“替换令牌任务”。有人说使用配置转换(它们不适用于 ASPNETCORE_ENVIRONMENT)。

不知道从这里去哪里。

我最近尝试过的:

为每个发布环境转换 Azure 网站部署的 web.config

基于Stage的Azure Pipelines配置转换

我在发布时生成的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> </environmentVariables> </aspNetCore> </system.webServer>
  

Azure 管道文件

name: WebApp-$(Date:yyyyMMdd).$(rev:rr) trigger: branches: include: - master variables: major: 2 minor: 0 patch: 0 solution: '**/*.sln' buildPlatform: 'Any CPU' buildConfiguration: 'Release' Parameters.WebsiteName: 'webapp' jobs: - job: Build pool: vmImage: 'windows-latest' steps: - task: DotNetCoreCLI@2 displayName: 'dotnet restore' inputs: command: 'restore' projects: '$(solution)' - task: VSBuild@1 inputs: solution: '$(solution)' msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="webapp" /p:EnvironmentName="Development"' platform: '$(buildPlatform)' configuration: '$(buildConfiguration)' - task: VSTest@2 inputs: platform: '$(buildPlatform)' configuration: '$(buildConfiguration)' - task: ArchiveFiles@2 displayName: 'Create Zip Archive' inputs: rootFolderOrFile: '$(build.artifactStagingDirectory)/' includeRootFolder: false archiveType: zip archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildNumber).zip replaceExistingArchive: true - task: DotNetCoreCLI@2 inputs: command: 'publish' publishWebProjects: true - task: PublishBuildArtifacts@1 displayName: 'Web Artifact'
    
azure .net-core azure-devops web-config azure-pipelines
2个回答
1
投票
当您在此处设置

environmentVariable

 时,如果以其他方式处理此问题,例如您在要部署应用程序的位置进行设置,则不需要此设置。您可以在文档中找到更多信息 - 在 ASP.NET Core 中使用多个环境
。但如果您仍然想通过 web.config 执行此操作,请检查此答案


0
投票

在最近的 .Net 版本中,他们添加了将以下属性添加到发布配置文件的功能。

<EnvironmentName>Release</EnvironmentName>

使用发布配置文件时,将使用适当完成的 ASPNETCORE_ENVIRONMENT 环境变量生成 web.config。

我在 Azure DevOps CI/CD 管道中使用这种方法,其中我的构建步骤如下所示,其中 DEV 与 Prod 的 PublishProfile 名称不同,这意味着我构建了两次,这很好,因为我在 prod 中以发布模式构建,无论如何在开发中调试。

- task: VSBuild@1 displayName: 'Build' inputs: solution: '$(solution)' msbuildArgs: '/p:DeployOnBuild=true /p:PublishProfile=MyApi-Prod.pubxml' platform: '$(buildPlatform)' configuration: 'Release' clean: true

我的工件发布的 bin 目录中的默认路径如下所示:

- task: PublishBuildArtifacts@1 displayName: 'Publish Prod Site' inputs: PathtoPublish: '$(System.DefaultWorkingDirectory)\src\MyWebApi\bin\Release\net6.0\publish' ArtifactName: 'ProdWebsite' publishLocation: 'Container'

此实例中的 Web 配置是

<?xml version="1.0" encoding="utf-8"?> <configuration> <location path="." inheritInChildApplications="false"> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="dotnet" arguments=".\MyWebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Release" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration>

希望这有帮助。
干杯

© www.soinside.com 2019 - 2024. All rights reserved.