【问题标题】:How to prevent msbuild config transforms in particular environments?如何防止特定环境中的 msbuild 配置转换?
【发布时间】:2018-07-03 02:45:57
【问题描述】:

我有一个 azure webjob 项目,它使用配置转换来创建开发/测试/发布配置。我们使用 TFS 将 CI/CD 部署到 Azure。我想让 MSBuild 为 dev 应用转换,以便我们可以在本地调试。但是,当我们在 CI/CD 管道中构建 TFS 时,我需要在构建步骤中禁用配置转换。

TFS 在发布步骤中有一个“应用 XML 转换”复选框,这是我们希望应用转换的地方,因为我们在发布期间设置了环境变量。不幸的是,这不起作用,因为在构建期间已经应用了转换,因此发布工件只有完成的输出文件,而不是单独的转换文件。

我已尝试编辑 .csproj 文件以禁用转换。我假设转换是由项目文件的以下部分执行的:

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="AfterCompile" Condition="Exists('App.$(Configuration).config')">
    <!--Generate transformed app config in the intermediate directory-->
    <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
    <!--Force build process to use the transformed configuration file from now on.-->
    <ItemGroup>
      <AppConfigWithTargetPath Remove="App.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>
  <!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.-->
  <Target Name="AfterPublish">
    <PropertyGroup>
      <DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
    </PropertyGroup>
    <!--Publish copies the untransformed App.config to deployment directory so overwrite it-->
    <Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
  </Target>

我尝试向这些部分添加“$(Configuration)|$(Platform)' == 'Debug|AnyCPU'” 之类的条件,但没有帮助(转换仍然适用于所有三个环境)。我什至完全评论了这一部分,我仍然得到了转换。这给我留下了三个问题:

  1. 如何禁用配置转换?
  2. 我怎样才能有条件地 禁用它们以便在 VS 中调试时仍然应用它们?
  3. 这是 正确的方法,或者有更好的方法来获得正确的 在 TFS 2017 中使用 CI/CD 时应用了转换?

【问题讨论】:

    标签: tfs msbuild


    【解决方案1】:

    要在构建期间禁用配置转换,您只需在构建任务的 MSBuild Arguments 部分添加参数 /p:TransformWebConfigEnabled=False。如果要在发布期间更新连接字符串,还需要添加/p:AutoParameterizationWebConfigConnectionStrings=False

    此外,如果您正在生成用于部署的 msdeploy 包,您需要更新您的项目文件,以便 Web.XXX.Config 文件将包含在包中。

    1. 将配置文件的“构建操作”从“无”更改为“内容”。
    2. 卸载您的项目文件并删除配置文件的&lt;DependentUpon&gt;标签。

    【讨论】:

    • 如果您将 MarkWebConfigAssistFilesAsExclude 属性添加为 false,您可以保留 DependentUpon 标记,这样您的转换将照常显示在 Visual Studio 内的解决方案资源管理器中,并且仍会复制到转换文件中。您可以将其作为 MSBuild 参数传递:/p:MarkWebConfigAssistFilesAsExclude=false
    • 谢谢,能够将它连接到 TeamCity 以阻止它使用 system.TransformWebConfigEnabled 的系统属性对 Web 配置执行转换。
    • 是否可以对服务的 App.config 执行相同的操作?我无法使用诸如 App.test-env.config 之类的环境配置进行设置...App.config 被重命名为 exe 名称 .exe.config 然后转换不适用于我的 App.test-env.config
    【解决方案2】:

    有了这些“MSBuild Arguments”,它对我有用:

    • /p:AutoParameterizationWebConfigConnectionStrings=false
    • /p:DeployOnBuild=true
    • /p:MarkWebConfigAssistFilesAsExclude=false
    • /p:PackageAsSingleFile=false
    • /p:PackageLocation="$(build.artifactstagingdirectory)\\"
    • /p:ProfileTransformWebConfigEnabled=false
    • /p:SkipInvalidConfigurations=true
    • /p:TransformWebConfigEnabled=false
    • /p:WebPublishMethod=文件系统

    重要的:

    • /p:AutoParameterizationWebConfigConnectionStrings=false
    • /p:MarkWebConfigAssistFilesAsExclude=false
    • /p:ProfileTransformWebConfigEnabled=false
    • /p:TransformWebConfigEnabled=false

    当我还添加时它开始工作:

    • /p:ProfileTransformWebConfigEnabled=false

    感谢@Eddie Chen - MSFT 和@Wessel T。

    问候汉斯

    【讨论】:

      猜你喜欢
      • 2014-03-03
      • 2017-12-01
      • 2013-08-16
      • 2014-04-25
      • 1970-01-01
      • 2020-08-02
      • 2017-12-08
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多