Microsoft Dynamics CRM Deployment Automation and Scheduling Automated Deployments using MSBuild scripts, batch files and other tools

Automated Deployment during the development of Dynamics CRM projects implementation is a time, effort and cost saving approach. A combination of MSBuild (MS Build) scripts, batch files and custom built console applications can provide a fully automated Dynamics CRM deployment and development life cycle.

Additionally, Microsoft Developer Toolkit which is released as a free tool part of the Dynamics CRM SDK provides an excellent deployment functionality (amongst many other useful features) but this is the subject of another post about automated deployment using Dynamics CRM SDK developer toolkit : http://www.mohamedibrahim.net/blog/2012/08/09/dynamics-crm-automated-deployment-using-dynamics-crm-developer-toolkit-from-the-sdk/ .

Examples of the most common deployment automation requirements in Dynamics CRM development projects:

1) Automated Deployment by getting the latest code from Visual Studio (via TFS – Team Foundaion Server) and deploying into a Dynamics CRM organisation on a server (local VM or a development/test server).

2) Export of Dynamics CRM default solution or a specific CRM solution package and importing the solution into another environment or Dynamics CRM server (or organisation). This is also known as Promotion: Promoting a CRM solution from one environemnt (such as Development environemtn) to another environment (such as test).

3) Export a CRM Solution from a CRM Server and then check in this exported solution file (the solution zip file) into TFS for backup or reusage.

These are the most commmon automated deployments used in projects but there could be many more based on projects requirements.

To achieve each on of these deployment automation setup there are a number of approaches and methods. I’ll try now to give some samples on how to do these automated deployments mentioned before:

You will need to create a batch file that calls an MSBuild file which in turn can do the operaton. The reason for using a batch file is that you can then setup a windows scheduled task to run the batch file at your chosen time for running the build. You can also add an auto numbering to the batch file if you want to increment the build number after every build. In this case, you will need to store the last build number in a separate text file.

The minimum that you will need in the batch file:

:: Check if Visual Studio command line is available. If not, then go to missing and end of file

if not exist “C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat” goto missing

:: Otherwise, call the Visual Studion Command shell.

call “C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat”

:: Call the msbuild file

msbuild myprojectbuild.msbuild /property:BuildVersion=%ver% /property:BuildDate=%builddate%>log\log_%builddate%_%ver%.txt

goto :eof

:missing

echo Visual Studio command prompt is missing

pause

goto :eof

 

Your MSBuild file that will get the latest code Visual Studio code from TFS can be too long but I’ll try to give you below the main commands that you will need to perform:

 <?xml version=”1.0″ encoding=”utf-8″ ?>

<!– All rights reserved for Mohamed Ibrahim Mostafa. www.mohamedibrahim.net Please leave this copyright notice if you use this file. –>

<Project xmlns=”http://schemas.microsoft.com/developer/msbuild/2003” InitialTargets=”Release”>

<PropertyGroup>

<!– Build version – alternatively these vallues can be passed from the batch file–>

<BuildVersion>1.1.0.0</BuildVersion>

<BuildDate>20120101</BuildDate>

<TFSUser>mytfsusername</TFSUser>

<TFSPassword>mytfspassword</TFSPassword>

<BuildConfiguration>Release</BuildConfiguration>

</PropertyGroup>

<Target Name=”Get”>

<!– get latest source code from tfs –>

<Exec Command=”tf get Clinet.Name/MySolutionName /force /recursive /noprompt /login:$(TFSUser),$(TFSPassword)” />

</Target>

<!– Build my visual studio CRM solution output binaries

<Target Name=”Build” DependsOnTargets=”Clean”>

<Message Text=”Building CRM solution code…” />

<!– Build all CRM-related outputs including plugins, scheduled tasks and user interface components –>

<MSBuild Projects=”Clinet.Name\MySolutionName\MyVisualStudio.sln” Properties=”Configuration=$(BuildConfiguration)”/>

</Target>

<!– now you have the visual studio solution built. use a console application to deploy it into CRM server as follows –>

<!– Use plugin registeration developer console application (available in SDK) to deploy plugins and workflows –>

<!– or alternatively use the deploy command of the microsoft dynamics crm developer toolkit (from the SDK) to deploy the whole solution into your CRM server organisation–>

<!– using the deploy command of the developer toolkit will be covered in a separate post –>

<!– The following command uses a custom console application that I have built to export Dynamics CRM solutions –>

<Target Name=”Package” DependsOnTargets=”Build”>

<Exec Command=”MyCustomDeploymentTool.exe export solution=Default outputdir=myfolder\CRM discoveryurl=$(DiscoveryUrl) orgname=$(OrganizationName)” />

<!– parameteres discovery url and organisation name are parameters in the property group –>

</Target>

<!–Now check in TFS and label your code using MSBuild commands –>

<Target Name=”Versioning” DependsOnTargets=”Package”>

<Exec Command=”tf add  /login:$(TFSUser),$(TFSPassword)” />

<Exec Command=”tf checkin MyProjectName\CRM_SolutionName_$(BuildDate)_$(BuildVersion).zip /login:$(TFSUser),$(TFSPassword)” />

<!– label TFS with the build number –>

<Exec Command=”tf label $(BuildVersion) MyProjectName/* /recursive /comment:&quot;Automatically labelled by Mohamed Mostafa build script.&quot; /login:$(TFSUser),$(TFSPassword)” />

</Target>

<Target Name=”Release” DependsOnTargets=”Versioning”>

<Message Text=”Completed..”/>    </Target>

</Project>

<!– All rights reserved for Mohamed Ibrahim Mostafa: www.mohamedibrahim.net –>

The console application that you can use to import or export a CRM solution will be the subject of another post. I think this post is too long already and I have spent good few hours writing it.

I hope this helps. If you have any questions, ideas or feedback please write them in a comment below.

If you want the various MSBuild files, batch files, etc then please comment below and I’ll email them to you (I tend to collect requests and send the files to a whole group in bulk).