26

I have asp.net core 2 application. Based on documentation i have created .pubxml file for my development environment. I am using web deployment (remote agent) to deploy the web package to target web server. On our build server where, jenkins is installed, i run the following command to build & deploy

D:\Jenkins\myproject\workspace\myapplication\Src\Api>dotnet publish Api.csproj /p:PublishProfile=development.pubxml

It builds and deploys the application successfully to target web server.

However my application has multiple appsettings files specific to each environment. For example appsettings.json, appsettings.development.json, appsettings.staging.json and appsettings.production.json The deployment process deploys all the appsettings files to web server. (in this case to development web server)

How do i only include environment specific appsettings file? (in this case i want to deploy appsettings.json, appsettings.development.json but not other 2 files)

2 Answers 2

41

You can add <ItemGroup> definitions to your publish profiles (.pubxml files) containing update statements for the files.

For example if the publish profile is named Production.pubxml/ Staging.pubxml according to the environment, you can add this item group to the root <Project> xml node:

<ItemGroup>
  <Content Update="appsettings.*.json" CopyToPublishDirectory="Never" />
  <Content Update="appsettings.$(MSBuildThisFileName).json" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
2
  • 2
    Worked for me. I just added <Content Update="appsettings.json" CopyToPublishDirectory="Never" /> to ignore the non-specific environment file.
    – AFract
    Commented May 4, 2021 at 7:48
  • Why isnt this done by out of the box? Seems like a security hole to publish production configs to staging etc?
    – Luke
    Commented Jun 10 at 14:30
9

I could handle this with the following *.pubxml:


<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
   .
   .
   .
  </PropertyGroup>
  <ItemGroup>
    <Content Update="appsettings.Development.json">
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.TestBed.json">
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

4
  • is this going to work on csproj file?
    – aj go
    Commented May 21, 2023 at 21:00
  • No, it is publish file with pubxml extension Commented May 22, 2023 at 9:46
  • thats disappointing. but thanks for the info there
    – aj go
    Commented May 23, 2023 at 0:46
  • 1
    Worked for me adding the <CopyToPublishDirectory>Never</CopyToPublishDirectory> to the csproj file. No more appsettings files when using "Publish". Thanks! Commented Jul 9, 2023 at 15:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.