In this short tutorial, I am going to narrate step by step process on how to create an AzureWebsite and publish a Visual Studio 2013 ASP.Net MVC Website using Microsoft Azure PowerShell. Even though we can perform the same operations from Azure Management Portal (and also through Visual Studio), we get more flexibility and control while automating deployment and maintenance tasks from PowerShell. One can create simple scripts which can perform end to end deployment activity (a deployment is not just deploying website, it usually involves creating Databases, Users etc.,).
Lets create a sample AzureWebsite by using following command –
New-AzureWebsite -Location "East US" -Name "intstringsdemo"
We can see PowerShell executed the command successfully –
On successful creation, we can navigate to Azure Portal and see our newly created site –
We can browse to the site –
Using New-AzureWebsite command, we can perform following operations as well. I will narrate below operations in separate tutorials.
- We can associate a Local Git and online Github Repositories for continuous deployments.
- we can specify alternate hostname like www.abc.com
Once an AzureWebsite is created, we can deploy our ASP.Net MVC code to it using PowerShell. First create a ASP.Net MVC4 Project in VS 2013.
Make sure we set Copy Local = True for following References (this is because these references are not copied to the Azure and when I try to access the AzureWebsite after deployment, I got missing references error)
Now we need to create a publish profile. Publish profile creation is a one time activity. Once created we can use the this publish profile to automate build activity using msbuild, Right click solution, select Publish.
Select Custom option.
Click OK and Select File System (select a appropriate folder).
Click Next and Select following options.
Click Publish. VS will successfully builds and publishes code to the selected folder.
Now to automate build process, we can use following build command. If msbuild command doesn’t work (I mean PowerShell doesn’t recognize msbuild command) in PowerShell console, then probably Environment variable might be missing for for .Net 4. So we can add C:\Windows\Microsoft.NET\Framework\v4.0.30319 to PATH environment variable.
msbuild C:\Users\venvemula\Desktop\IntstringsDemo\IntstringsDemo.sln /p:DeployOnBuild=true /p:PublishProfile=Test
Above command will publish the VS project to the local file system (as stated in publish profile). After a successful build, we get following message (there will be many interim messages to show build progress) –
As a next step, we need to package the published folder to zip file and move the file to AzureWebsite.
To Zip the folder content, use below commands –
$source = "C:\users\venvemula\desktop\pub" $destination = "C:\users\venvemula\desktop\pub.zip" Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]::CreateFromDirectory($Source, $destination)
Once we have Zip File, we can upload the contents to AzureWebsite using following Azure Cmdlet –
Publish-AzureWebsiteProject -Package C:\Users\venvemula\Desktop\pub.zip -Name intstringsdemo
On successful publish, we have following messages displayed on PowerShell Console.
Now we can visit the AzureWebsite, and see our upload site using following command –
Show-AzureWebsite -Name intstringsdemo
To delete an AzureWebsite, we can use following command –
Remove-AzureWebsite -Force -Name intstringsdemo
Here I am going to wrap this tutorial. In next set of tutorials, we are going to see Github Automated Deployments, Load-balancing with Traffic Manager, Streaming Logs etc., Stay Tuned!!! Happy Coding!!!