I was recently at a client where they had an azure cloud service project in a Git repository in Visual Studio Online (VSO). They asked me if they could use Team Build (TFS build) to build the solution and then have the build kick off a Release Management release template which would deploy the Azure cloud service app into Azure. Without even pausing, I replied, “Of course! What could possibly go wrong?” Apparently a lot. But in my defense, this all made perfect sense to me… in theory. If I used an on premises build controller and agent and a slightly tweaked build template, I should be able to have my build definition pull code from Git, compile everything and kick off a Release Management (RM) release template. This template would “deploy” the app to a proxy machine where it would then run a power shell script to publish the azure service to azure. Should be cake. Like I said earlier, what could possibly go wrong? Ok, so here is the plan. In order to do all this, I need the following:
- An on premises build controller (with on premises agents) which connects to VSO.
- A build template which pulls code from a git repository that will trigger a new Release. Lucky for me, Release Management update 2 ships with an updated Release command line tool which makes this possible.
- A team build which builds an azure cloud service package (*.cspkg).
- A Release Management template which uses power shell scripts to deploy the *.cspkg to an azure service.
Setting up an on prem build controller and agent
Setting up an on premises build controller and agent for VSO is ridiculously easy.
- Load the TFS 2013 media and launch tfs_server.exe.
- The Team Foundation Server Setup dialog is displayed. Select I accept the license… and click Install Now.
- TFS installs.
- Team Foundation Server Configuration Center dialog is displayed. Click on Configure Team Foundation Build Service on the left hand side tree. And click Start Wizard.
- At the welcome screen click Next.
- At the Team Project Collection panel, click the browse button.
- This brings up the Connect to Team Project Collection dialog. Click on Servers.
- This brings up the Add/Remove Team Foundation Server dialog. Click on Add
- This brings up the Add Team Foundation Server dialog. In here, enter the the name of your VSO and click ok. In my case, it is abelsquidhead.visualstudio.com.
- This brings up the Microsoft account sign in. Enter your username and password (to VSO) and click sign in.
- After authenticating, you get brought back to the Add/Remove Team Foundation Server dialog. Click Close.
- This closes the dialog and you get brought back to the Connect to Team Project Collection dialog where you are logged into your VSO instance with the user whose credentials you entered in step 10. Select the Team Project Collection you want this build controller to be registered to and click Connect. In my case, I only have on collection called Default Collection
- This takes you back to the Select a Team Project Collection wizard panel with your collection information filled in. I have previously configured a build controller and agent. That’s why the wizard says it found 1 build controller and 1 agent. If this is your first time registering controllers and agents, this would be 0.
- This takes us to the Build Services configuration panel. Go ahead and select Scale out build services. Select the number of agents you want to run on this build machine and select Create New Build Controller in the add capacity drop down. Click Next.
- This take you to the Settings panel. Enter the service account which will run the builds and click Next.
- At the Review Panel, review the settings and click Next.
- At the Readiness Check, if everything is green, click Configure.
- At the Configure panel, wait for the configuration to finish and click Next.
- And voila. You are done. Click Close.
That’s it for configuring an on prem build controller and agent. You literally just walk through the wizard and you are done.
Creating a build template which triggers a new Release
To create a build template which triggers a new Release, I used this blog here: http://blogs.msdn.com/b/visualstudioalm/archive/2014/04/04/release-management-continuous-deployment-with-visual-studio-online-and-any-ci-systems.aspx In a nutshell, you take a build template (since I needed to pull code from a Git repository, I started with the template GitTemplate.12.xaml), then you modify it by adding steps to trigger a release using the command line tools. Luckily, someone has already done all that so all we need to do is just merge those changes into your build template. Here is a copy of the snippets file to use: ReleaseCMDTemplate.12.Snippets.xaml And here is a link to instructions on how to merge your build process template with the snippets. http://blogs.msdn.com/b/visualstudioalm/archive/2013/12/09/how-to-modify-the-build-process-template-to-use-the-option-trigger-release-from-build.aspx And finally, here is the build template I created to use a Git repository and to trigger a new Release: GitRMTemplate.12.xaml Now create a build definition using this build template.
- Upload the build template somewhere into VSO or TFS. It just needs to be in the same collection as your on prem build controller.
- Bring up Team Explorer in visual studio, connect to your TFS or VSO and click on Builds. I’m using VSO with a Git repository.
- This will bring up the Team Explorer Build page. Click on New Build Definition.
- This will open new build definition wizard in the main panel with the General sub tab selected. Give your build definition a name. I named mine AzureRMBuild. Then click on Trigger.
- On the trigger page, select how you want your build to be triggered. I wanted mine to be a Manual build so I selected Manual. Now click on Source Settings.
- In the Source Settings page, select what source you want to pull down. For this example, I’m just pulling the master branch down. After choosing what source you will get, click on Build Defaults.
- In the Build Defaults page, chose the offset build controller you just set up, and select the staging location. For my example, I wanted to store the drop location locally on prem so I selected Copy build output to the following drop location. After filling out this page, click on Process.
- On the Process page, click on the Show details drop down.
- This will open up Build process file section. Click on New.
- This opens up the Browse dialog where you click Browse and browse to the build template (.xaml) file you uploaded to VSO earlier. Remember, I’m using a Git repository.
- This will load the new build process template. Go ahead and select the solution/projects you are going to build, and set the property ReleaseBuild to true. In the Release Template field, put in the name of the Release template which this build will trigger.
- Now press save in Visual Studio.
Building an azure cloud service package (*.cspkg) and cloud service configuration (*.cscfg) from your solution
The end goal is to have Release Manager deploy the azure service to azure. Publishing to Azure can be done using power shell scripts if you have the .cspkg and .cscfg files for an azure service. And building these files can easily be done with a Windows Azure Cloud Service solution.
If you only have a web project, convert it to a Windows Azure Cloud Service solution. Once you have a cloud service solution, in order to generate the *.cspkg and *.cscfg you will need to do two things:
- Pass in the correct MSBuild parameters to build template so the build will build the *.cspkg and *.cscfg
- Modify your Cloud Service project so that it copies the *.cspkg and *.csfg files to the right directory.
Passing in the MSBuild parameters to the build template for Team Build
In the build definition, pass in /t:Publish /p:TargetProfile=Cloud /p:AutomatedBuild=true as MSBuild arguments.
Modify your project by adding a target which copies the *.cspkg and *.cscfg files to the output directory.
- Open up your Azure Cloud Service solution in visual studio. Right click your cloud service project and select Unload
- Now right click the unloaded Azure Cloud Service project and select Edit
- This will open up the .ccproj file in the main editing pane. Scroll to the bottom and right before </Project>, copy the following:
<Target Name=”CopyPackageToDropLocation” AfterTargets=”CorePublish”
DependsOnTargets=”CorePublish” Condition=”$(AutomatedBuild) != ””>
<Message Text=”Copying package and config file to drop location.” />
- Save the file , right click the .ccproj in the Solution Explorer and select Reload Project. Save everything and check it back into VSO/TFS
Creating a Release Management template which uses power shell scripts to deploy the *.cspkg to an Azure service
In order to create the Release Management template, we need to do 3 things:
- Create a powershell script which will deploy an azure .cspkg to Azure.
- Install and configure Release Management Server, Client and Deployer
- Create and configure the deployment template
Creating a powershell script which deploys an Azure .cspkg to Azure
I won’t bore you with my troubles of creating this powershell script but after some searching, a whole lot of hair pulling, and some tweaking, here is my script. Given a service name, storage account name, .cspkg file, .cscfg file, environment, subscription name, and .publishsettings file, this power shell script will deploy the .cspkg to Azure. PubAz.ps1
Installing and configuring Release Management Server, Client and Deployer
We need to install Release Management Server somewhere on our network. And because our build will be kicking off a Release Management template, we need to install the Release Management client on our build agent box. And finally, Release Management will need to “deploy/copy” our Azure build to a proxy machine and then, it will use power shell scripts to deploy the Azure service into Azure. Therefore, we need to install the Release Management deployer/agent on the proxy machine. For this demo, I am using an all in one VM, so I will be installing the RM Server, Client and Deployer/Agent onto the same VM that has by build controller and build agent.
Installing the Release Management Server
- Load media with Release Management with update 2 and double click rm_Server.exe
- Click I agree with the licensing terms… and click Install.
- After the install finishes, click the Launch button
- The install dialog closes. The Configure Release Management Server dialog opens. Enter the account that will run Release Management and type in the database to use for Release Management. And click apply settings.
- Configuration completes, click the Close button.
- Click the Close button on the Configure Release Management dialog box and the RM server install is finished.
Installing the Release Management Client
- Load the Release Management with update 2 media and run rm_Client.exe
- In the Release Management Client dialog, select the I agree with the license dialog and click Install.
- Release Management Client installs. Click the x in the corner to close the dialog.
Installing the Release Management Deployer/Agent
- Load the Release Management with update 2 media and run Rm_DeploymentMsdn.exe.
- In the Deployment Agent install dialog, click on I agree to the license terms and click install.
- After the install completes, click Launch
- This brings up the Configure Microsoft Deployment Agent 2013 dialog. Enter in the service account information and enter the url to the Release Management Server and click Apply settings.
- In the Configuration Summary dialog, click the Close button.
- In the Configure Microsoft Deployment Agent 2013 dialog, click the close button.
Creating and configuring the deployment template
Creating and configuring the deployment template will consist of 8 steps.
- Configure the Release Management client
- Adding a new tool which will deploy a *.cspkg to Azure using the powershell script we saved earlier.
- Adding the proxy server to Release Management
- Creating an Environment which has the proxy server
- Adding a Stage for your deployment
- Configure a Release Path for your azure deployment
- Adding a Release Management deployment component which uses the new Azure deployment tool you created in step 2
- Adding a Release Management deployment component which copies the *.cspkg and *.cscfg files to a known location
- Configuring a new Release Template which uses a build drop from TFS and deploys the Azure service to Azure
- Configure the build to trigger the Release Template
Configuring the Release Management Client
If this is the first time you have opened the Release Management client you will need to configure the client.
- Open Release Management Client
- the Configure Service dialog opens. Enter the RM server name and click the ok button.
Add a new Tool which uses your powershell script to deploy *.cspkg to Azure into the Release Management inventory
- Copy PubAz.ps1 to somewhere on the Client machine. I copied the file to my desktop.
- Open Release Management Client
- Click on the Inventory tab, then click the Tools sub tab. This will bring up a list of all the tools.
- Click the New button.
- This opens up the new tools panel. Enter a name for your new tool. I named mine AzureDeployTool and click Add under Resources
- This brings up the Open dialog box where your browse to where you saved the Azure Deploy power shell script and click Open.
- This adds the script as a resource to this tool. Click the Save and Close Button.
- This adds the AzureDeployTool in the list of tools in RM
Adding the proxy server to Release Management
- Launch Release Management Client.
- Click on the Configure Paths tab and then click on the Servers sub tab.
- This will bring up the Servers panel. Click on the New button.
- This will bring up the New Server panel. Enter A Name for your server and also the DNS Name. In my case, the DNS name is RMAzureTest and click the Save & Close button
- This will bring you back to the Servers panel. Make sure that the newly added server has a Status of active
Creating an Environment which has the proxy server
- Click on the Environments sub tab.
- This brings up the Environments panel. Click on New.
- This brings up the New Environment panel. Fill in a Name for the Name and select an Owner. The click the Link Existing button.
- This brings up the Servers dialog box. Select the server you just added from the list and click the Link button.
- This closes the dialog and adds the server to the list of servers in your environment. Click Save & Close.
Adding a Stage for your deployment
Since this is only a POC, I am not going to bother with multiple stages like Dev, QA, UAT and Prod. I’m just going to create one stage called Azure.
- Click the Administration tab.
- Click on the Manage Pick List sub tab.
- This brings up the Manage Pick List panel. Click on the Add Button.
- This adds a new blank row for a stage. Type in a name (I chose Azure) and click the save button
Configure a Release Path for your Azure deployment
Because this is just a POC, our release path will only go through one stage. The Azure stage.
- Click on the Configure Paths tab, then click on the Release Paths sub tab.
- This will open the Release Paths panel. Click on New.
- This will open the New Release Path panel. Enter a name for this release path. I used ReleaseToAzure. Then click the Add button under Stages.
- This will add a new stage. Fill in your Stage information and click the Save & Close button.
Adding a Release Management deployment component which uses the new Azure deployment tool
- Click on Configure Apps.
- This will bring up the Configure Apps > Release Templates page. Click on the Components sub tab.
- This brings up the Components panel. Click on New.
- This brings up the new Component panel. Fill in the name for this component and in the UNC Path to Package, add the machine name of where the drop location will be. Then click on the Deployment tab in the bottom tab control.
- This will display the Deployment tab. In the Tool drop down, select the azure tool you created earlier.
- Enter the following into the Arguments text box. Then, click the Save & Close.
-command ./PubAz.ps1 -serviceName __ServiceName__ -storageAccountName __StorageAccountName__ -packageLocation __Package__ -cloudConfigLocation __Configuration__ -environment __Environment__ -subscriptionDataFile __PublishSettings__ -Selectedsubscription __Subscription__
- This brings you back to the Components panel. Your new component should be in the list.
Adding a Release Management deployment component which copies the *.cspkg and *.cscfg files to a known location
- Click Configure Apps tab and then Components sub tab, then click the New Button.
- This brings up the New Component panel. Enter a name for your new component and enter the machine name which holds the drop location for the UNC Path to Package. The click the Deployment tab.
- This brings up the Deployment tab. In the Tool drop down, select XCopy Deployer and click Save &Close.
Configuring a new Release Template which deploys a Azure Service build to an Azure Service
To create my release to azure template, I want release Manager to do the following:
- Copy the *.cscfg and *.cspkg file from the build drop location to a known location on my proxy server
- Call my powershell script which deploys the *.cspkg to Azure.
Here are the parameters the powershell script needs:
- Azure service name – In my example, my service name is DemoWebApp2
- Azure storage account used by the service – In my example, the azure storage account for this service is demowebapp2
- The path to the *.cspkg file – When Release Management copies the *.cspkg file to the proxy server, it will copy it to cscfg C:DeploymenAzureTestapp.publishDemoWebApp2.cspkg
- The path to the *.cscfg file – When Release Management copies the *.cscfg file to the proxy server, it will copy it to C:DeploymenAzureTestapp.publishServiceConfiguration.Cloud.cscfg
- The Azure Environment – In my example, the environment is Production
- The path to the *.publishsettings file. This file holds the credential information to the Azure account and needs to be saved to a known location on the proxy server. Because this file holds credential information, it can be placed in a folder where only the service account running Release Management has permissions to access it. In my example, I copied my *.publishsettings file to C:Deployazure2.publishsettings
- Name of the subscription – In my example, the name of my Azure subscription is Windows Azure MSDN – Visual Studio Ultimate
To download your .publishsettings file, visit this link and log in using the Live Id associated with the Azure subscription.
https://windows.azure.com/download/publishprofile.aspx For this POC, I configured the simplest release template. There is no error handling or rollbacks. This template is just a quick and dirty proof that a template can deploy to an Azure service. To configure the quick and dirty Release Template, do the following:
- Click on the Configure Apps Tab and then the Release Templates sub tab, then click the New button.
- This will open the Release Template Properties dialog. Give your new Release Template a name and then in the Release Path drop down, select the path you just created and click Create.
- This brings you to the Azure Demo Template panel. To add our components to this template, right click the Components node in the Toolbox tree and select Add.
- This will bring up the Components dialog. Multi select (shift + click) both components we created for this POC and click Link.
- This will bring you back to the AzureDemoTemplate panel. The two components will now be under the Comonents node.
- Click and drag the proxy server under the Servers node (I named my proxy server AzureProxy) under the Servers node and drag it into your Deployment Sequence
- I am going to assume the folder C:DeploymenAzureTest exists and this is where Release Management will copy the bits fro m the build. Drag the component CopyAzureFiles under Components to the AzureProxy box.
- Click the expand arrow on the CopyAzureFiles title bar to open the activity.
- This expands the CopyAzureFiles activity. In the Installation Path field, type in C:DeploymenAzureTest.
- Drag the DepAzService Component as the next step in the AzureProxy server.
- Click on the expand activity arrow.
- This opens up the DepAzService activity. Fill in the variables with your values. For me, I set the following:
- ServiceName: DemoWebApp2
- StorageAccountName: demowebapp2
- Package: C:DeploymentAzureTestapp.publishDemoWebApp2.cspkg
- Configuration: C:DeploymentAureTestapp.publishServiceConfiguration.Cloud.cscfg
- Enviornment: Production
- PublishSettings: C:Deploymentazure2.publishsettings
- Subscription: Windows Azure MSDN – Visual Studio Ultimate
- Click Save & Close.
- Close Release Management Client
Configure build to trigger the Release Template
One last step. We need to make sure our build definition uses the Release Management template we just created. From Visual Studio, connect Team Explorer to your VSO Git repository and edit your build definition. In the properties tab under the Release section, make sure the property Release Template is the name of the Release Management template that you just created. I named mine AzureDemoTemplate.
That’s it. You are done. You now have a on prem build which pulls a project from VSO, builds it and triggers a Release Management template that deploys the build into an Azure service.