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

3

我目前正在研究和实施一种自动化部署MVC和WebApi项目到生产环境的方法。

到目前为止,我已经成功地使用Visual Studio 2012中内置的发布工具。通过在IDE中运行它,我可以使正确的文件和IIS配置在生产服务器上运行。但是,我希望通过命令行或PowerShell来实现相同的效果,这样一个脚本就可以运行,它将自动部署这两个网站。

我尝试过运行像这样的命令行脚本:

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

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


有没有完整的源代码示例,可以作为最终解决方案? - Kiquenet
1个回答

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"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接