Deploying an Azure Website using PowerShell


Every developer learns that F5 is their deployment friend, but when it comes to deploying to live/production environments it is not an option. So how can you website code be deployed into Azure?

Let’s start of by creating a website in Visual Studio.

  • Open Visual Studio and Click New Project… from the welcome window.
  • From the new project dialog, select ASP.NET web Application and Click OK.

  • On the New ASP.NET Project dialog, select the template you wish to use. I’m using an MVC template, with No Authentication.

  • At this point it will ask you to configure your Microsoft Azure Website. Fill in the details and click OK. (I know this creates the site for you in Azure, we will delete this later.)

Once created you will have a Website, just underneath the solution we have PowerShell files that Visual Studio uses to deploy the website to Azure. It is these PowerShell files and configuration that we will use ourselves.

First we need to package up the website. You can do this by right clicking on the project in Visual Studio and click Publish.

The Publish web dialog that appears will be set up for direct publishing to Azure, we need to change the Publish method to Web Deploy Package, and then give a Package Location.

Click Next > and on the next page of the wizard, ensure you Configuration is set to Release. Note: if there is a web job related to this, then do not click Exclude files from the App_Data folder as this is where the webjobs get compiled to.

Click Next > then Publish.

If you open the folder where you published your website files to you will find a collection of files. The only file you need from here is the zip file, so delete the rest, they are for IIS deployment.

From your Visual Studio project copy over your configuration, Publish-WebApplicationWebsite.ps1 and AzureWebSitePublishModule.psm1 file. These are all the files required to give to your administrator to deploy to Azure, with the next set of instructions with the heading “Deploying to Azure”.

Just before we continue to the next step, go to you azure tenant and delete the website that Visual Studio created, that way you will be doing everything via PowerShell.

Deploying to Azure.

To deploy to Azure you need to ensure you have Azure PowerShell installed on your machine – see my blog Installing Azure PowerShell if you need instructions how to. You will also need to know how to connect to you Azure Subscription via PowerShell. The steps below will show the basic commands but if you have multiple subscriptions you should read my blog Handling Azure PowerShell with multiple Azure Subscription first to ensure you don’t deploy to the wrong subscription.

  • Open the Configurations folder and edit the .json file.
  • Change the website Name and Location to match where you want this deployed and the name you wish to give it.

{

    "environmentSettings": {

        "webSite": {

            "name": "CFWebSiteDemo-Dev",

            "location": "West Europe"

        },

        "databases": [

            {

                "connectionStringName": "",

                "databaseName": "",

                "serverName": "",

                "user": "",

                "password": "",

                "edition": "",

                "size": "",

                "collation": ""

            }

        ]

    }

}

  • As you can see from the script above, I’ve named my CFWebSiteDemo-Dev. Personally I find it useful to copy this file once for each environment (Dev, test, uat, prod), then when I’m deploying I just need to call the right configuration file.

  • Open up an Azure PowerShell Window.
  • Connect using your account. Add-AzureAccount press enter. You will be asked to sign into your Azure account.
  • Type Get-AzureSubscription to see all the subscriptions linked to your account(s). You need to ensure that your subscription you want to use is your current subscription.
  • Type Select-AzureSubscription –SubscriptionId [SubscriptionID]. This should now make your subscription the current subscription. Call Get-AzureSubscription to make sure. Read Handling Azure PowerShell with multiple Azure Subscription if you are still having issues up to this point.
  • Now navigate to your directory in the Powershell window. Mine is at c:\deploy. Cd deploy
  • Now type ./publish-webApplicationWebSite.ps1 –Configuration [Location of correct environment.json file] –WebDeployPackage [Location of zip file]
  • This will create the website in Azure, and then install the files. You will get a screen very similar to below.


NOTE: If you encounter an error when the website is trying to be created, then it will not upload the files. I have had this a few times before. Just check that the Azure website exists in your Azure Tenant and seems to be looking OK, then call the .\Publish-WebApplicationWebSite.ps1 command again. (You might need to try a new Azure PowerShell window.) Other reason why it fails could be due to how your company locks down your network. If you are having no success, try on a network (Starbuck wireless) that doesn’t have a network policy. If it then seems to work on that network, then you have something preventing the upload in your network, and will need to work through the problem with your network administrators.

  • As you can see in my tenant I now have a web app called CFWebSiteDemo-Dev.

  • My Website is up and running.

 

Upgrading Azure Website.

Upgrading the Azure website is very easy, make your changes and then create a new publish zip file.

Use the original PowerShell files and configuration files for Deploying Azure follow these steps.

  • Open up the Azure Windows PowerShell as Administrator.
  • Ensure you have added the account and subscription to your PowerShell profile if you haven’t done the previous steps.
    • Get-AzureAccount –Checks if account is there.
    • Get-AzureSubscription – Checks if Subscription is there.
    • Select-AzureSubscription –SubscriptionName [subscriptionName] –Selects the Subscription to deploy to.
  • Deploying files to Azure
  • Type ‘./Publish-WebApplicationWebsite.ps1 –Configuration [Location of correct environment .json file] –WebDeployPackage [Location of new zip file]
  • Your updated website should now be up and running.
Advertisements