Msbuild 使用VS代码发布Web部署

Msbuild 使用VS代码发布Web部署,msbuild,webdeploy,visual-studio-code,Msbuild,Webdeploy,Visual Studio Code,在Visual Studio中,我使用“发布web”功能执行一些web.config转换,并将WebAPI项目发布到服务器。发布是使用Web部署完成的 现在我正在使用VisualStudio代码,我已经丢失了该工具。但是,我想继续使用WebDeploy发布项目有没有办法编写一个VSCode任务来发布我的项目? Visual Studio Publish使用[target].pubxml文件。我有“staging.pubxml”和“production.xml”。这些文件看起来像MSBuild文件

在Visual Studio中,我使用“发布web”功能执行一些web.config转换,并将WebAPI项目发布到服务器。发布是使用Web部署完成的

现在我正在使用VisualStudio代码,我已经丢失了该工具。但是,我想继续使用WebDeploy发布项目有没有办法编写一个VSCode任务来发布我的项目?

Visual Studio Publish使用[target].pubxml文件。我有“staging.pubxml”和“production.xml”。这些文件看起来像MSBuild文件。所以,也许这只是从代码执行msbuild任务的问题。不过,我不知道从哪里开始


另一个想法是,我可以启动一个Web部署命令行工具。我从来没有使用过,而且第一个想法似乎更好。

Visual Studio代码没有像Visual Studio那样的集成构建系统(Web发布)。但它确实有命令行任务运行和内置Git

因此,您有两个选择:

1) 使用任务运行程序从命令选项板(ctrl+p)开始生成/发布。在预览*中提供。这需要手动编写脚本,但一旦完成,就很容易从该点开始执行任务

(更新:提及其他兼容的任务运行程序,包括:Make、Ant、Gulp、Jake、Rake或MSBuild,.settings tasks.json提供了如何使MSBuild文件工作的示例。按ctrl+p键入:“运行任务”,然后单击“配置任务”)

2) 为持续集成设置源代码管理系统,以便在将更新推送到特定分支时,它将运行MSBuild(或其他生成系统)脚本并为您发布到服务器。我们使用Team Foundation Server(TFS)和Git。我们有一个特定的“release/master”分支,它被设置为在收到推送时构建和发布。它也需要一些初始配置,但一旦完成,它是自动的。如果没有TFS,请联机尝试TFS。还有很多其他的选择,但这就是我们所使用的

我的处境和你想弄明白的一样。我很想知道你发现了什么


*据报道。尽管查看tasks.json文件,但预览中似乎有Gulp和MSBuild示例。

要使用MSBuild发布,您需要使用以下命令:

msbuild <Project or Solution Path> /p:DeployOnBuild=true /p:PublishProfile=<Publish Profile Name>
msbuild/p:DeployOnBuild=true/p:PublishProfile=
  • 您可以指向解决方案,这将发布包含有效发布配置文件的所有项目:

    msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=Test
    
    msbuild\MySolution.sln/p:DeployOnBuild=true/p:PublishProfile=Test
    
  • 您可以指向如下所示的特定项目:

    msbuild <FullPath>\Project1\MyProj.csproj /p:DeployOnBuild=true /p:PublishProfile=Test
    
    msbuild\Project1\MyProj.csproj/p:DeployOnBuild=true/p:PublishProfile=Test
    
  • 在这两种情况下,您还可以指定使用.pubxml文件的完整路径:

    msbuild <FullPath>\MySolution.sln /p:DeployOnBuild=true /p:PublishProfile=<FullPath>\PublishProfiles\Test.pubxml
    
    msbuild\MySolution.sln/p:DeployOnBuild=true/p:PublishProfile=\PublishProfiles\Test.pubxml
    
事实上,*.pubxml文件是MSBuild脚本,因此您可以像处理任何其他MSBuild脚本一样处理它,例如,您可以从命令行替换属性,如下所示:

msbuild <FullPath>\Project1\MyProj.csproj /p:DeployOnBuild=true /p:PublishProfile=Test
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\"

文件系统
释放
任何CPU
假的
C:\Deploy\MyProject\
真的
msbuild\MySolution.sln/p:DeployOnBuild=true/p:PublishProfile=\PublishProfiles\Test.pubxml/p:publishUrl:“D:\Differentitpath\Differentitfolder\”
您可以从持续集成服务器或任何其他生成脚本使用这些命令

其他信息:


假设您现在正在使用最新的vscode(1.7.x)。您可以使用VisualStudio代码的任务运行程序

首先,您需要通过按
并输入
task
来配置任务运行程序。选择任务:配置任务运行程序

vscode将创建一个新文件
tasks.json
,其中包含以下内容

{
    // 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"
        }
    ]
}
其次,现在需要添加新的发布任务。根据@Rolo给出的答案,您可以在
tasks
数组中添加新任务:

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

第三,一旦
tasks.json
完成。您可以通过按
Ctrl
+
P
(或在Mac上按
Cmd
+
P
)来使用
publish
任务,并键入
task publish

您可以使用
dotnet publish
命令将其部署到本地或远程目录

对于本地目录

dotnet publish -c Release -o ./publish
dotnet publish -c release -p:PublishDir=//serverName/d$/inetpub/wwwroot/appName
用于远程目录

dotnet publish -c Release -o ./publish
dotnet publish -c release -p:PublishDir=//serverName/d$/inetpub/wwwroot/appName
dotnetpublish
在幕后调用MSBuild

-c configName
=>定义生成配置

-o dirName
=>指定输出目录

-p:PublishDir
=>指定复制输出目录的目录

要了解
dotnet发布
命令,请访问


您也可以使用命令行在VS代码中使用。

我认为最困难的部分是找到正确的命令行。看看这个博客。如果您找到了正确的命令行,我可以帮助您在VSCode for Itts中定义一个任务。Dan,感谢您的这些想法。我已经能够将我的Grunt脚本与命令调色板集成。而且,我认为在MSBuild中也不难做到这一点。但我的问题是,我不知道执行这些发布XML文件的命令行是什么样子。这就是我被困的地方。对于您的第二个想法,这可能是正确的方法,我将把它作为一个长期目标,因为我们已经在使用TFS。感谢您提供的信息,但问题是关于从Visual Studio代码部署,而不是从命令行部署。我将根据原始问题的内容为您提供此答案因此,也许这只是从代码执行msbuild任务的问题。不过,我不知道从哪里开始。”这是一条评论“我认为最困难的部分是找到正确的命令行。