Friday, September 21, 2012

Moving a website to Azure while adding Continuous Deployment from Git

Moving a website to Azure while adding Continuous Deployment from Git:
I've got this little one page website I wrote a few years ago that attempts to detect the version of the .NET Framework you have and give you a download for the smallest redist you need to get up to date. It's called SmallestDotNet.  It was written and thrown up while watching an hour of TV. Fortunately or unfortunately this little throwaway still gets about 10,000 page views a month. It also has a tiny JSON API that a lot of people have actually embedded into their sites and products.
Additionally, it's starting to break. IE9+ doesn't include .NET Framework details in its HTTP User-Agent. The site isn't setup for .NET 4.5, etc. You get the idea. Plus, it's a pile of if statements and prayer and it's just horrible. Worse yet, it lives in a ZIP file on my hard drive and I drag it into SmartFTP to deploy. Yes, I know, this is all thick with irony.
I took and hour over lunch today to accomplish these basic goals:
  • Get this crap site into source control so others could help fix it
  • Move the site to Azure and off my blog's server
  • Update the site's DNS
  • Setup continuous deployment with Git to Azure
  • Fix the .NET 4
  • Maybe add .NET 4.5 detection
  • Cleanse myself, possibly by reading Programming Pearls

Making a Site in Azure

I use the Azure Command Line Tools (get them with npm install azure). (I did an npm update azure --global to make sure I had the latest.)
I clone my Git Repository, create an Azure site, then make sure my Azure site is setup as a remote. (Soon the remote add will be made automatically when creating an Azure site from within a Git repro)
git clone https://github.com/shanselman/SmallestDotNet.git

azure create site SmallestDotNet
I copied my site in from elsewhere and
git add .

git commit -m "Initial Commit"
Next, I'll associate my Azure site with my GitHub (or CodePlex) repo. I could also deploy from local files, but I like the idea of an auto-deploy since I'll guessing I'll be making lots of changes and this is one less thing to worry about.
From the Deployment tab on the Azure Portal I'll use OAuth to authorize azure with GitHub.
Associate a GitHub Repository
Then...
Authorize Windows Azure?
I'll confirm the repository I'm using:
Setup Publishing and Select the Repository to Publish
This sets up a WebHook plugged into GitHub so it'll notify Azure when a push happens, Azure will deploy.
Now, I'll push my repository to origin.
git push origin master 
Within a second or so of this push, the site automatically updates and is up and running at smallestdotnet.azurewebsites.net. I'll do a custom domain in a second.
Yes, I should probably make a deployment branch. I could have two small Azure sites and then have each represent a deployment from a different branch in Git. Then I could git push staging and git push production.
Animated Gif showing a Deployment slide into the queue
To move the domain name records from my own server to azure, I'll log into my DNSimple account (referral link for a free month!) and create a CNAME that points to my new site.
Creating a CName and an A Record to point to Azure
Back in the Azure Management Portal - about 10 minutes later - I'll click Manage Domains and enter my new CName.
image
I'm also using an A record so read the instructions carefully.  After Azure verifies the CNAME, I return and add the A record. That way I can have not just www. but also the "naked" domain work as well.
I visit the site, and boom. It's there. http://www.smallestdotnet.com/ is on Azure in less than a lunch hour. About 25 minutes, actually. Whew.
Next, I have to fix the horrible code and bugs. Join me and issue a pull request.  ;)
Thanks to Calin Balauru for his help with the code a few months back.
Related Reading: ScottGu on Continuous Deployment in Azure with Git


© 2012 Scott Hanselman. All rights reserved.



DIGITAL JUICE

No comments:

Post a Comment

Thank's!