Thewads
Thewads

Reputation: 5053

Automate Web Deploy to remote IIS server

I am currently in the process of investigating and implementing an automatic method of deploying our MVC and webApi projects to a production environment.

So far I have used the Publish tool built into Visual Studio 2012 to good effect. By running this in the IDE I can get the correct files and IIS config up and running on the production server. I am however, wanting to achieve the same as this, but via command line or powershell so a script can just be run, and it will deploy the 2 websites automatically.

I have tried to do this by running a command line script such as:

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

and this builds the project and creates a package in the obj folder of the project. Is this the correct approach, and if so, how can I make this package be automatically deployed to the remote server?

Upvotes: 4

Views: 4051

Answers (1)

David Brabant
David Brabant

Reputation: 43459

Here is the function I use whenever I have to deploy a web application:

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

Upvotes: 4

Related Questions