Friday 21 December 2012

Cloud Recipes: Build and Deploy an Azure Web Role with CC.net and GitHub

In this recipe we will create an Azure Web Role (Cloud Service Project), add the solution to a Git Repository, Download CruiseControl.Net server and set-up a build and deployment script to build from the Git Repository and deploy to the Azure Web Role.

To complete this recipe you need the following:
  • The latest release of Cruise Control.NET (1.8.2.0) - Download and install here 
  • A Microsoft Azure Subscription - sign up for a three month free trial here.
  • A Git Account - sign up for a free account here.
  • Putty Gen and Putty Agent can be downloaded here.
  • Git  
  • The .Net framework 4 or above
  • The Azure SDK 1.8 and all the goodies
  • Windows Azure Authoring Tools  
  • Windows Azure Tools for Visual Studio 
  • Visual Studio IDE 2012
I created this recipe on one machine with everything installed in one place. In the real world you would have a dedicated build server.


Create a Git Hub Repository

To start, create a Git Hub repository. If you aren't willing to pay for access you will only be able to create a public repository. 
  • Log onto http://github.com and select your git hub account. 
  • Click 'Create a Repository'
  • Enter your Repository Name and Create

Create an Azure Cloud Service Web Role and push to Github

Next we will create an Azure Cloud Service Project with a ASP.Net MVC 4 Web Role and save locally. We will then push up to the Git Repository. The repository will ultimately be used by CC.Net to pull the latest code changes, build the solution and deploy to Azure. 

  1. Create a Windows Azure Cloud Service project in Visual Studio 2012 
  2. Add an ASP.Net MVC Web Role to the Solution. Give your Role a better name like 'MyAzureWebRole.Web'
  3. Select the Internet Application project template
  4. Don't worry about creating unit tests for the project. 
A good folder structure and naming convention is, for example:

\Mark.Rodseth.MyAzureWebRole
--\Build {build scripts, certificates, targets, tools}
--\Documentation {doxygen docs}
--\Tools {dev tools}
--\Solutions
----\MarkRodseth.AzureWebRole.Cloud {cloud configuration project folder }
----\MarkRodseth.AzureWebRole.Web { MVC Web Project folder } 
----\MarkRodseth.AzureWebRole.Web.Tests { Web Tests folder }
----\MarkRodseth.AzureWebRole.sln {solution file}

Once created initialise your Git Repository and push up to Git. 
  1. Initialise your local Git repository. GitBash > git init
  2. Initialise Git Hub remote repository. GitBash > git remote add origin {Url to Git Repository}
  3. Add the contents of the folder to staging. GitBash > git add .
  4. Commit the changes. GitBash > git commit -m "my first commit"
  5. Pull the latest from the Git Hub Repo: GitBash > git pull origin master
  6. Push the changes to Git Hub. GitBash > git push origin master
You will notice that step 5 fails because you trying to push to git using SSH but haven't configured the public / private key for secure communication. 

Set-up SSH Public / Private Key Pairs 


To enable secure communication between your client and Git Hub you need to generate a SSH (Secure Shell) Public and Private Key Pair and add the public key details to Git Hub. This will allow your git client and Git Hub to talk securely.  SSH keys will also be used by the CC.Net server for encrypted communication with GitHub.

Generate Public / Private Key Pair
  1. Load PutyGen.exe
  2. Click Generate and wave your cursor randomly around the Key area
  3. Add a passphrase to private key. 
  4. Save Public Key \users\{username}\.ssh\{publickeyname}.pub
  5. Save Private Key \users\{username}\.ssh\{privatekeyname}.ppk
Add the Public Key to GitHub SSH Keys List. 
  1. Navigate to GitHub>AccountSettings>SSH keys and click Add Key
  2. Give your public ssh key a friendly name
  3. Copy and Paste the contents of your public key saved in the above step into the text area
Your Git Hub account is now configured to accept secure communications from Git clients using the associated Private key. 

If you try do a git push now you will still get the same error. To allow seamless communication to GitHub you need to run Putty Agent and add the SSH Private Key you created. This agent will funnel all SSH communication through it and handle the authorisation step and negate having to re-enter your private key pass phrase everytime you push to GitHub. Putty Agent is required because CC.Net server will be blocked if a pull from GitHub requires manual entry of the Private key pass phrase.  

  1. Run Putty Agenty - pageant.exe
  2. Double click the Putty Agent icon in your system tray. This will launch a GUI where you can add your Private Key.
  3. Click Add Key and navigate to the SSH Private Key you created.
  4. Enter the pass phrase. 
Once that is done, you or CC.Net can push and pull from GitHub securely and not be bothered with pass-phrase prompts.

You can now push your changes to git hub: GitBash > git push origin master.

Create Azure Web Service and download Publish Profile

In this step you will log onto your Azure Management Dashboard, create an Azure Web Service, and download your Subscription Publishing Profile. The Publishing Profile will be used by CC.Net to publish to your Azure Instance.
  1. Log into the Azure Management Portal 
  2. Create a new Cloud Service by specifying your URI A-Record and Affinity Group (which data center the Web Role will be hosted.) 
  3. Download Publishing Profile. Navigate to https://windows.azure.com/download/publishprofile.aspx which will automatically download the Publishing Profile for your subscription. A publishing profile contains your Azure Subscription ID, Management Certificate Public Key and the URL to the Azure Management API. This information is used by clients when communicating with the Azure Management API. Save the Publsih Profile somewhere for use in the next step.  

Configure CC.Net and PowerShell

Now that you've set-up up Source Control (Git & GitHub), created an Azure Cloud Project with an MVC 4 Web Role, and created an Azure Subscription, you are ready to configure your CC.Net build server and create a new CC.Net project which will build and deploy the solution to your Azure Service.
  1. Copy the MS Build Microsoft.WebApplication.targets file from a Visual Studio installation to the build server. The targets file is an XML File which is used by MsBuild to execute the common steps in creating a package for web site deployment. The file is located in the following directory:  C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications 
  2. Save the Publishing Profile created previously to a location on your build server. e.g.  C:\CCNetBuild\PublishProfiles\myazure-credentials.publishsettings. You will reference this Publish Profile in your PowerShell deploy script. 
  3. Make sure you have the Azure Powershell Script Template installed in your CC.Net Build folder. e.g. C:\CCNetBuild\Scripts\ . The Powershell Script Template can be downloaded here
  4. Make sure the PowerShell is able to execute the script by setting the PowerShell execution policy  to RemoteSigned or UnRestricted. Run Windows Powershell and enter the following command: Set-ExecutionPolicy RemoteSigned.
  5. Add a new CC.Net project and Build Trigger, Source Control, Build Task (MSBuild) and Deploy Task (Powershell) to ccnet.config (Located in the CC.Net server folder) . See screenshot for configuration example.
  6. Update PowerShell Script Variables. See screenshot for  PowerShell script example.
ccnet.config project configuration. 


PowerShell Script

Once all this is setup then you can navigate to the CC.Net Dashboard and run your Build and Deploy project in CC.Net and watch as you web application magically makes its way onto the cloud. Also, any pushes/changes to the Git Repository will be detected and the CC.Net project will download the latest changes from Git Hub, build the project and deploy the changes up to your production or staging slot. 

There were a few niggles and stack overflow posts I had to trawl through to get it fully working. I ended up running the MSBuild and Powershell commands directly to iron out a few kinks like path errors or naming errors.

If you experience issues then here are a few links which I found helpful. 

11 comments:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as

    updated one, keep blogging… Azure Online Training

    ReplyDelete
  2. Tech-Rash: Cloud Recipes: Build And Deploy An Azure Web Role With Cc.Net And Github >>>>> Download Now

    >>>>> Download Full

    Tech-Rash: Cloud Recipes: Build And Deploy An Azure Web Role With Cc.Net And Github >>>>> Download LINK

    >>>>> Download Now

    Tech-Rash: Cloud Recipes: Build And Deploy An Azure Web Role With Cc.Net And Github >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete