32

In Visual Studio, I use the "publish web" feature to do some web.config transforms, and publish a WebAPI project to our server. The publishing is done using Web Deploy.

Now that I'm using Visual Studio Code, I've lost that tooling. But, I'd like to continue publishing the project using Web Deploy. Is there some way to write a VSCode task that will publish my project?

Visual Studio Publish uses a [target].pubxml file. I have "staging.pubxml" and "production.xml". These look like MSBuild files. So, maybe it's just a matter of executing an msbuild task from Code. Not sure where to start with that, though.

Another thought is that I could kick off a Web Deploy command line tool. I've never used that, and it seems like the first idea would be better.

AJ Morris
  • 1,099
  • 2
  • 10
  • 19
  • I think the hardest part is finding the right command line. Check this blog out http://www.gregpakes.co.uk/post/msbuild-publish-profiles-and-web-site-projects. If you find the right command line I can help you with defining a task in VSCode for it – Alex Dima May 02 '15 at 19:50

4 Answers4

20

Assuming you are using the latest vscode now (1.7.x). You can use Visual Studio Code's Task Runner.

First, you would need to configure the task runner by pressing <F1> and enter task. Select Tasks: Configure Task Runner.

A new file tasks.json would be created by vscode with following content.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "msbuild",
    "args": [
        // Ask msbuild to generate full paths for file names.
        "/property:GenerateFullPaths=true"
    ],
    "taskSelector": "/t:",
    "showOutput": "silent",
    "tasks": [
        {
            "taskName": "build",
            // Show the output window only if unrecognized errors occur.
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

Second, now you would need to add the new publish task. With the answer given by @Rolo, you can add a new task in the tasks array:

    {
        "taskName": "publish",
        // Always show errors from builds.
        "showOutput": "always",
        "args": [
            "/p:DeployOnBuild=true",
            "/p:PublishProfile=Test"
        ]
    }

Third, once the tasks.json is completed. You can use the publish task by pressing Ctrl+P (or Cmd+P on Mac), and type task publish.

yuxhuang
  • 5,279
  • 1
  • 18
  • 13
  • can't reproduce your answer. I believe you were using v1 of Task, and now Tasks 2.0 is available [microsoft Task documentation](https://code.visualstudio.com/docs/editor/tasks) – Razvan Mar 31 '18 at 16:23
  • @Răzvan and anyone else interested. Here's Microsoft's [Convert from "0.1.0" to "2.0.0"](https://code.visualstudio.com/docs/editor/tasks#_convert-from-010-to-200) guide for VS Code Tasks. – Jeremy Cook May 21 '18 at 17:22
  • I don't have option `Tasks: Configure Tasks Runner` – Falcon Stakepool Mar 15 '23 at 09:20
5

Visual Studio Code does not have an integrated build system (Web Publish) like Visual Studio does. But it does have command line task running and Git built in.

So you have a couple of options:

1) Use a task runner to kick off your build/publish from the command palette (ctrl+p). Grunt is available in the preview*. This requires that you manually script it out, but once that is done, it is easy to kick off the task from that point.

(UPDATE: the docs mention other compatible task runners including: Make, Ant, Gulp, Jake, Rake or MSBuild -- AND the .settings tasks.json has examples of how to get your MSBuild files working. Press ctrl+p type: "run task" and then click "configure tasks")

2) Setup your source control system for continuous integration so that when you push an update to a specific branch, it will run the MSBuild (or other build system) scripts and publish to the server for you. We use Team Foundation Server (TFS) and Git. We have a specific "release/master" branch that is setup to build and publish when it receives a push. It also takes some initial configuration, but once complete, it is automatic. If you don't have TFS, try TFS online. There are many other options out there, but that's what we use.

I am in the same position as you trying to figure this one out. I would love to know what you find out.

*According to the Deep Dive session at Build 2015. Although looking at the tasks.json file it looks like Gulp and MSBuild examples are available in the Preview.

Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
  • 1
    Thanks for those ideas, Dan. I've been able to integrate my Grunt scripts with the command palette. And, I don't think it's hard to do the same with MSBuild. But my problem is that I don't know what the command line would look like for executing those publish XML files. That's where I'm stuck. For your second idea, that's probably the correct way to do it, and I'm going to make it a longer-term goal, since we are already using TFS. – AJ Morris May 09 '15 at 12:26
5

In order to publish using MSBuild you need to use the following command:

msbuild <Project or Solution Path> /p:DeployOnBuild=true /p:PublishProfile=<Publish Profile Name>
  • You can point to a solution, this will publish ALL the projects that includes a valid Publish Profile:

    msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=Test
    
  • You can point to a specific project like this:

    msbuild <FullPath>\Project1\MyProj.csproj /p:DeployOnBuild=true /p:PublishProfile=Test
    
  • In both cases you can also specify use the full path to the .pubxml file:

    msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=<FullPath>\PublishProfiles\Test.pubxml
    

In fact the *.pubxml files are MSBuild scripts, so you can interact with it like you do with any other MSBuild script, for example you can replace the properties from the command line like this:

Test.pubxml

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <WebPublishMethod>FileSystem</WebPublishMethod>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish />
        <ExcludeApp_Data>False</ExcludeApp_Data>
        <publishUrl>C:\Deploy\MyProject\</publishUrl>
        <DeleteExistingFiles>True</DeleteExistingFiles>
    </PropertyGroup>
</Project>

    msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=<FullPath>\PublishProfiles\Test.pubxml /p:publishUrl:"D:\DifferentPath\DifferentFolder\"

You can use these commands from your continuous integration server or any other build scripts.

Additional information:

Command Line Deployment

Rolo
  • 3,208
  • 1
  • 24
  • 25
  • Thanks for the info, but the question is about deploying from Visual Studio Code, not from command line. – Madara's Ghost Nov 28 '16 at 16:30
  • I'm giving you this answer based on the content of the original question **_"So, maybe it's just a matter of executing an msbuild task from Code. Not sure where to start with that, though."_** and this from a comment **_"I think the hardest part is finding the right command line. Check this blog out gregpakes.co.uk/post/…. If you find the right command line I can help you with defining a task in VSCode for it – Alex Dima"_**. Isn't this an option for you anymore? – Rolo Nov 28 '16 at 20:23
  • I'm (semi) aware of the option of using comman-line tasks in vs code, but I was honestly wondering, given that VSCode is a Microsoft product (OS, but still), if it had a specific integration for it. – Madara's Ghost Nov 28 '16 at 23:30
3

You can use dotnet publish command to deploy it to a local or remote directory.

For local directory

dotnet publish -c Release -o ./publish

For remote directory

dotnet publish -c release -p:PublishDir=//serverName/d$/inetpub/wwwroot/appName

dotnet publish calls MSBuild behind the scene.

-c configName => Defines the build configuration.

-o dirName => Specifies the output directory.

-p:PublishDir=> Specifies the directory where output directory is copied

To learn dotnet publish command, visit link.

You can also use publish profile in VS Code using command line.

Circuit Breaker
  • 3,298
  • 4
  • 17
  • 19