How to publish solution in nuke-build using msBuild

2.2k Views Asked by At

This might be a lame question, maybe I'm missing something very obvious. But I'm trying to solve this for a few days now without success.

I have quite a huge solution (31 projects) that uses .NET framework. We used to use CAKE build for build and deploy process, but now I have to move that process to nuke-build. I'm using MSBuild, not DotNet.

I managed to convert Tasks (from CAKE) to Targets (Nuke) and fixed dependencies among different Targets. Solution builds.

But I can't find simple way to use target "Publish" on MSBuild and don't know how to set publish directory.

For example one of tasks in CAKE looked like this:

Task("BuildService")
    .Does(() => {
    var csprojFile = rootSolutionDirectory + "/Core/MyProject.Service/MyProject.Service.csproj";
    var publishDestination = $"{tempPublishDestination}/service";
    MSBuild(csprojFile, new MSBuildSettings()
        .WithProperty("target", "build;publish")
        .WithProperty("DeployOnBuild", "true")
        .WithProperty("PublishProfile", publishProfile)
        .WithProperty("Configuration", configuration)
        .WithProperty("Platform", platform)
        .WithProperty("PublishUrl", publishDestination)
    );
});

I converted that to Target in NUKE:

Target BuildService => _ => _
    .Executes(() =>
    {
            var csprojFile = rootSolutionDirectory + "/Core/MyProject.Service/MyProject.Service.csproj";
            var publishDestination = $"{tempPublishDestination}/service";
            MSBuild(s => s
                .SetTargetPath(csprojFile)
                .SetTargets("Rebuild", "Publish") // looks like "Publish" does nothing
                .SetConfiguration(Configuration)
                .SetAssemblyVersion(GitVersion.AssemblySemVer)
                .SetFileVersion(GitVersion.AssemblySemFileVer)
                .SetInformationalVersion(GitVersion.InformationalVersion)
                .SetMaxCpuCount(Environment.ProcessorCount)
                .SetNodeReuse(IsLocalBuild))
                // don't know where to put publishDestination
                ;             
    });

I'm stucked here. I don't know how to tell MSBuild to also do "Publish" through Nuke.Common.Tools.MSBuild.MSBuildSettings or through some other object.

And also, how to tell MSBuild WHERE should publish it.

I'll be very thankfull for any answer, best regards, Martin

2

There are 2 best solutions below

0
On

Thanks to Matthias I managed to solve this issue (see Comments on question).

Here is my solution, if anyone else would have simmilar problem:

    Target BuildService => _ => _
    .Executes(() =>
    {
        var csprojFile = RootDirectory + @"\Core\MyProject.Service\MyProject.Service.csproj";
        var publishDestination = $@"{TempPublishDestination}\service";

        MSBuildTasks.MSBuild(s => s
            .SetTargetPath(csprojFile)   
            .SetConfiguration(Configuration)
            .SetMaxCpuCount(Environment.ProcessorCount)
            .SetNodeReuse(IsLocalBuild)
            .SetProperty("DeployOnBuild", "true")
            .SetProperty("PublishProfile", Configuration.ToString())
            .SetProperty("Platform", "AnyCPU")
            .SetProperty("PublishUrl", publishDestination)
            .SetTargets("Build", "Publish")
            );
    });

Thank you

0
On

If you want to avoid having a publish profile:

MSBuild(s => s
    .SetTargetPath(<cs proj directory>)   
    .SetConfiguration(Configuration)
    .SetMaxCpuCount(Environment.ProcessorCount)
    .SetNodeReuse(IsLocalBuild)
    .SetProperty("DeployOnBuild", "true")
    .SetProperty("Platform", "AnyCPU")
    .SetProperty("PublishUrl", <a folder>)
    .SetProperty("DeployDefaultTarget", "WebPublish")
    .SetProperty("WebPublishMethod", "FileSystem")
    .SetTargets("Build", "Publish")
);