59

I'm trying to call "dotnet publish" with a specific publish profile pubxml file as documented here :

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.1&tabs=aspnetcore2x

However, everything I try seems to result in the default behaviour, and the /p:PublishProfile part is ignored.

dotnet publish /p:PublishProfile="MyFolderProfile"

Doesn't work, and logs no error, building to the default location under "/obj".

I do note that an intentionally incorrect command has the same result though, eg:

dotnet publish /p:PublishProfile="MyFolderProfile-XXXXX"

What am I doing wrong? - Any pointers would be greatly appreciated!

Lanorkin
  • 7,310
  • 2
  • 42
  • 60
Sam
  • 1,208
  • 1
  • 9
  • 16
  • Of course, it's not a solution, but I ended up listing all options (Release, output directory, etc.) manually – Felix May 26 '18 at 20:39
  • Have you tried specifying the project the profile was built upon? Like this: `dotnet publish WebApplication.csproj /p:PublishProfile="MyFolderProfile"` – Victor Alves Aug 01 '18 at 15:56
  • 3
    @VictorAlves I'm not the original poster, but I have tried with the project in the command line and that doesn't help either. It seems the problem is a long existing one, even using msbuild: https://github.com/Microsoft/msbuild/issues/1901 It seems you will have to manually specify the parameters on the command line, e.g. `-c Release`, `/p:PublishDir=dist`, etc – Falconne Aug 22 '18 at 05:08
  • 4
    I only ever run into these issues with microsoft software. – aaaaaa Feb 06 '19 at 09:14
  • 1
    See my answer: https://stackoverflow.com/a/62954314/73573 – palindrom Jul 17 '20 at 12:57

8 Answers8

22

My response is late. My solution has a simple .NET Core console application ConsoleAppCore.csproj. I used Visual Studio IDE to generate a publish profile with the name FolderProfile.pubxml and then the following commands worked for me:

Relative path - From the solution root

dotnet publish ConsoleAppCore\ConsoleAppCore.csproj /p:PublishProfile=ConsoleAppCore\Properties\PublishProfiles\FolderProfile.pubxml

Absolute path - From any location

dotnet publish "C:\work\ConsoleAppCore\ConsoleAppCore.csproj"   "/p:PublishProfile=C:\work\ConsoleAppCore\Properties\PublishProfiles\FolderProfile.pubxml"

On Azure dev ops

Task name=.NET Core

Task version=2

Command=publish

Path to projects=I left this empty

Arguments=

$(System.DefaultWorkingDirectory)\ConsoleAppCore\ConsoleAppCore.csproj /p:PublishProfile=$(System.DefaultWorkingDirectory)\ConsoleAppCore\Properties\PublishProfiles\FolderProfile.pubxml  --configuration $(BuildConfiguration) --output  $(Build.ArtifactStagingDirectory)\ConsoleAppCore-Publish\

In the Azure Dev Ops build pipeline scenario, I have redirected the output to a folder under $(Build.ArtifactStagingDirectory) . I also have a Publish Artifact task which is configured to use the staging directory variable.

I have made use of the publish profile XML file because I wanted a single file to govern the complete behavior while on Azure Devops. Relying on a single file for all parameters simplifies management on Azure.

Azure Dev ops - Artifacts Explorer

The Publish Artifact task created a drop for me and this is how it looks. Please notice that the file name in the explorer tallies with the name specified along with the --output option in the dotnet publish task enter image description here

Sau001
  • 1,451
  • 1
  • 18
  • 25
  • 2
    Unfortunately this does not seem to work the same way on linux. The publish profile is simply ignored. – Ivaylo Slavov Nov 25 '19 at 15:39
  • I can't speak for Linux but this is the most surefire way to get it to wok on Windows. – Taul Jan 22 '20 at 16:38
  • The "Relative path - From the solution root" solution worked for me! Thanks – Jan Mar 17 '21 at 08:02
  • 3
    Looks like full / relative paths should not be working now, as per https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish "If you specify a path and file extension when setting the `PublishProfile` property, they are ignored. MSBuild by default looks in the _Properties/PublishProfiles_ folder and assumes the _pubxml_ file extension. To specify the path and filename including extension, set the `PublishProfileFullPath` property instead of the `PublishProfile` property." – Lanorkin Oct 26 '21 at 13:11
16

I ran into the same issue a while ago. I managed to fix it by instead calling:

dotnet build [projectname] /p:PublishProfile=[PublishProfile]
A. Don
  • 201
  • 2
  • 6
  • 2
    What will `[projectname]` be? – Anders Lindén May 06 '19 at 14:55
  • 6
    Add `/p:DeployOnBuild=true` to deploy after build - see here: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-3.1#publish-profiles – shlgug Dec 16 '19 at 15:09
11

There is an open (as of Oct 2021) issue for Folder Publish with the similar symptoms https://github.com/dotnet/sdk/issues/12490

Workaround I use for now is to manually edit *.pubxml file produced by VS.Net to have both PublishUrl and PublishDir keys with the same values.

With this dotnet publish -p:PublishProfile=... works as expected.

Lanorkin
  • 7,310
  • 2
  • 42
  • 60
10

You might need the full path, e.g.

dotnet publish -c Release /p:PublishProfile=Properties\PublishProfiles\FolderProfile.pubxml
andrewb
  • 5,200
  • 6
  • 36
  • 54
4

Try this based on: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-3.1#publish-profiles

dotnet build ..\project\project.csproj /p:DeployOnBuild=true -c Release /p:PublishProfile="Folder Staging"

I included a relative csproj path and a publish profile name with a space in it to clarify how to deal with those cases. The -c flag is optionally used to specify the configuration to use.

shlgug
  • 1,320
  • 2
  • 11
  • 12
3

See https://github.com/dotnet/docs/issues/19677. The PublishProfile is only the name, any directory before it is disregarded.

PublishProfile property is treated as a file name and the directory is constructed separately.

The PublishProfileFullPath property should be used if specifying a custom path.

James Esh
  • 2,219
  • 1
  • 24
  • 41
1

I used this on VS build events

dotnet publish $(SolutionDir)DIRECTORI_1/PROJECT_1.csproj -c Release -o C:\Deployments\FOLDER_1
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Javier Contreras
  • 871
  • 7
  • 16
1

To solve the problem you must add a < PublishDir > property to your .pubxml file. Then the

dotnet publish /p:Configuration=Release /p:PublishProfile=FolderProfile

command works fine.

Example below.

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <DeleteExistingFiles>false</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>C:\MyApp\PUBLISH</PublishUrl>
    <PublishDir>C:\MyApp\PUBLISH</PublishDir>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <_TargetId>Folder</_TargetId>
  </PropertyGroup>
</Project>

you can also use a relative path like

    <PublishUrl>PUBLISH</PublishUrl>
    <PublishDir>PUBLISH</PublishDir>
ejectamenta
  • 1,047
  • 17
  • 20