自动将Web部署自动部署到远程IIS服务器

时间:2013-02-11 10:17:51

标签: powershell command-line msbuild visual-studio-2012 webdeploy

我目前正在调查和实施将MVC和webApi项目部署到生产环境的自动方法。

到目前为止,我已经使用Visual Studio 2012中内置的Publish工具取得了良好的效果。通过在IDE中运行它,我可以获得正确的文件,并在生产服务器上配置和运行IIS。但是,我想要实现与此相同的功能,但是通过命令行或powershell可以运行脚本,它将自动部署2个网站。

我尝试通过运行命令行脚本来执行此操作,例如:

msbuild MyProject.csproj /p:DeployOnBuild=true;PublishProfile=MyProfile

这将构建项目并在项目的obj文件夹中创建一个包。这是正确的方法吗?如果是这样,我该如何将此软件包自动部署到远程服务器?

1 个答案:

答案 0 :(得分:3)

以下是我必须部署Web应用程序时使用的功能:

function MSBuild-Publish-Web-Service
{
    param (
        [parameter(Mandatory = $true)][string] $WebProjectFile,
        [parameter(Mandatory = $true)][string] $DestinationDir
    )

    Write-Host "`t`t$($MyInvocation.InvocationName): Publishing web service from $SourceDir"

    try
    {
        $Error.Clear()
        $MsBuildPath = "$env:Windir\Microsoft.NET\Framework\v3.5\MSBuild.exe"

        if (!(Test-Path $MsBuildPath))
            { throw "Could not find $MsBuildPath" }

        $res = [string](. $MsBuildPath "$WebProjectFile" /verbosity:minimal "/t:ResolveReferences;_CopyWebApplication;publish" /p:Configuration=Debug /p:OutDir="$DestinationDir\bin\" /p:WebProjectOutputDir="$DestinationDir")

        if ($res.Contains("error"))
            { throw $res }
    }

    catch
    {
        Write-Error "`t`t$($MyInvocation.InvocationName): $_"
    }

    Write-Host "`t`t$($MyInvocation.InvocationName): Web service published"
}