MS Web Deploy with Bamboo

  2016-09-26


Building with msbuild

First things first - create PowerShell script myapp.build.ps1 to build your app:

mdbuild.exe myapp.web.csproj `
    /p:Configuration="Debug" `
    /p:BuildProjectReferences=true `
    /T:Package `
    /p:OutDir="c:/build/myapp"

Please note that you should be building .csproj file, NOT .sln. Once your build is finished it should produce a deployment package, which is basically a zip file with your application binaries and other stuff.

Deploying with msdeploy

Deploying with msdeploy.exe is simple once you know how to do it. Create a myapp.deploy.ps1 which will deploy your web app:

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

msdeploy.exe `
    -verb:sync `
    -source:package="c:/build/myapp/\_PublishedWebsites/myapp.web_Package/myapp.web.zip" `
    -dest:'auto,computerName="https://myapp.scm.azurewebsites.net:443/msdeploy.axd?site=myapp",userName="$myapp",password="mypassword",authtype="Basic",includeAcls="False"' `
    -disableLink:AppPoolExtension `
    -disableLink:ContentExtension `
    -disableLink:CertificateExtension `
    -setParamFile:"$dir\example.com.SetParameters.xml"

Create example.com.SetParameters.xml file in the same folder and configure all required web.config parameters in it. These parameters will be used for the web.config transform when deploying.

<?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name="IIS Web Application Name" value="myapp" />
  <setParameter name="myappdb-Web.config Connection String" value="Data Source=tcp:abcdefg.database.windows.net,1433;Initial Catalog=myappdb;User Id=admin@abcdefg;Password=mydbpassword;" />
</parameters>

Hook it up with Bamboo

Build plan

Before starting the msbuild, we want to make sure that all NuGet packages are restored. Normally this step is not needed, because msbuild will do this for you by default, however in certain cases not all packages are restored. I am still not sure why this happens, but to avoid the trouble altogether we forcefully restore all packages as so:

bamboo build plan

Then we run the msbuild task to build our ASP.NET MVC project.

bamboo build plan

When the build is finished all output files should be stored in an artifact. Artifact is basically a zip file which is stored inside Bamboo’s database. Each build will generate a new artifact and later that artifact can be accessed. This means that we can redeploy historic builds easily.

Because artifacts are stored indefinitely, we should keep them as slim as possible, by copying only the required files.

bamboo build plan

Deployment project

Our deployment project will consist of 2 steps:

  • Extract build artifact into a temporary folder on the build server
  • Deploy artifact with msbuild script

First let’s choose the artifact we need as so:

bamboo deployment

Then we can use our previously prepared deployment script as in the screenshot below. The argument value is: -Package ${bamboo.build.working.directory}\myapp.web\outdir\_PublishedWebsites\myapp.web_Package\myapp.web.zip

bamboo deployment

22bugs.co © 2017. All rights reserved.