ASP.NET MVC Deployments to Azure via GitHub
Windows Azure keeps getting better and better. It’s been available for over 3 years and most of that time I have glossed over it in favor of Amazon Web Services. The big discouraging factors for me were cost, limited services and the horrible Silverlight management interface it used to have. A lot has changed in Azure over the last few years and I continue to be impressed.
They finally dumped the Silverlight management interface and went to a modern (and awesome, actually) HTML5 interface. It is very functional and has some impressive UX features. I like it a lot. Also, Azure has fully embraced Git. To be honest, this surprises me because Microsoft has traditionally been so closed and promoting of their own tool chain (i.e. TFS for source control). But, this seems to be in line with a broader shift I’ve seen in the Microsoft Developer Division in recent years. Kudos to Microsoft because I think this is the right direction.
Anyway, I recently found that you could** tie an Azure website to a GitHub repository for deployments**. This is way cool because I love me some GitHub and this integration makes deployments so easy.
Here’s an example of how you do it:
Create ASP.NET MVC Solution
Normally you’ll already have a solution you’re working with you want to use but for demonstration purposes I will create new ASP.NET MVC 4 Solution called foo and save it locally.
Create Git Repository and Push to GitHub
You can (obviously) do this with the Git command line or one of the Git GUIs but you basically just want to init a new local Git repo and push it to a new remote GitHub repo. I usually start out by going directly to GitHub and creating a new repo and then following the instructions to connect it with a local repo.
After you create the GitHub repo, there will be a screen that shows how to init a local repo and connect it to your new GitHub repo and it looks similar to this:
cd C:\dev\foo git init git commit -m "first commit" git remote add origin firstname.lastname@example.org:username/foo.git git push -u origin master</pre>
Setup new Azure Website
If you know how to use the Azure Management interface then you know this is easy. Just click “Web Sites” on the left bar, then click “New” at the bottom left. Click “Quick Create”, give it a name and then you are good to go. It will initialize and be ready in a matter of seconds.
Link Azure Website to GitHub
You’ll want to pull up the new web site you just created and then go to the “Dashboard” page. On the right side under “quick glance” you should see an option called “Setup deployment from source control”. Click this, select GitHub as the source and then select your repo. Follow the screens and your ASP.NET MVC website should be automatically deployed to Azure within seconds. Also, each time you update the GitHub repo it will trigger a new deployment on Azure!
A Note About Dependencies
When Azure pulls your source from GitHub and compiles it, it will have to resolve any assembly references. There are two ways to handle this:
- Check your packages folder (from the solution directory) into GitHub – This is straight forward and the least issue prone way to handle references. Just ensure you commit the packages folder which contain all your assembly .dll references. If all your external dependencies were added from NuGet you should be good to go. If you have any assembly references that are not in NuGet you can place them in the packages folder directly and change the reference to point to this location. This way, when Azure compiles your site, all the assembly .dll files will be available in the packages folder and you’ll be set.
- Enable NuGet Package Restore – To avoid checking in the packages folder (which could be quite large and go against everything that has been pounded in your head about source control best practices), you can Enable NuGetPackage Restore which will tell Azure to download package references from NuGet. You need to make sure and commit the *packages.config *file from your project directory so Azure will know which packages to install.