Blog.Amit Apple

Blog by Amit Apple

Sending an email when your Azure web app (website) deployment completes

Windows Azure Web Sites have a new support for web hooks, currently the only event that is directly being invoked is called PostDeployment and it will be invoked whenever a deployment is complete with the result of that deployment.

You can find more information on the API for this feature here.

Cool Stuff with Web Hooks

Having this new API allowed us to collaborate with zapier.com and allow developers to get this post deployment information in many ways like: email, phone call or sms or even tweet about it.

About Zapier

zapier is a service that is all about the integration of other different services through the concept of triggers and actions.

A trigger could be a new github issue was opened or a new email was sent to your gmail account and an action could be tweeting a new tweet to tweeter or create a new note in your Evernote account.

In our case we've created a new trigger on Zapier for a deployment to a website that is complete and using Zapier you can connect this trigger to any of actions that are available.

More on Windows Azure Web Sites - Zapier Service

Let's see how:

Prerequisites

  • A Microsoft Azure Web App that is deployed using source control (git, mercurial or dropbox).

  • Sign up to Zapier

  • Have both the Zapier and Azure portal open

Steps

  • Go to Zapier and create a new zap.

  • For the trigger service select Microsoft Azure Web Apps

  • For the trigger select New Website Deployment

  • For the action, we'll select email and send outbound email for this sample but any can be selected

  • Click Continue

  • We need to connect to our Azure Web App, for this we need one piece of information from the Azure portal

  • In the Azure portal go to your website, click on the CONFIGURE tab and under the git section copy the url which is under the DEPLOYMENT TRIGGER URL

  • Go back to the Zapier site and paste this url to the Deployment URL textbox, enter a name for this website account and click continue.

  • Since the email action doesn't require any special account, just click continue again.

  • At this point you can filter what kind of post deployment event will actually trigger this action (for example only failed deployments), for now we keep this empty, click continue

  • Create your Outbound Email lets you customize the email, the content can be static and dynamic (coming from the post deployment result)

  • On the To textbox enter your email

  • On the Subject textbox enter: Deployment complete with status: {{status}}

  • On the right side of each textbox there is an icon you can click to get the different dynamic fields that will be available from the deployment result, so in the Body textbox just experiment with the different fields.

  • Continue

  • Try out your Zap lets you test your zap by getting previous deployment results and doing the selected action on them, you can use it if you have existing deployments on your site otherwise click skip this.

  • Name your zap and make it live.

  • Last step is to deploy your site and see the magic (action) happens.

Get more help about Windows Azure Web Sites on Zapier.

Read more...

Run tests during Azure Web Apps (Websites) deployment

Deploying websites to Azure using GIT has never been easier, and now a new support for running .NET unit tests was added.

To run tests during website deployment you'll need to customize your deployment by generating a deployment script, this is described in the following article.

In a nut shell:

  • Make sure you have node.js installed on your machine.
  • Open a command shell window.
  • Install azure-cli (if you don't have it yet) using the command: npm install azure-cli -g.

Now that prerequisites are there, go to your root repository directory in the same command shell and generate the custom deployment script.

For an asp.net web application run the following command:

azure site deploymentscript --aspWAP -s

For example:

azure site deploymentscript --aspWAP MyWebApp\MyWebApp.csproj -s MyWebApp.sln

This will generate a deploy.cmd file under the root of your repository, we need to update this file in order to make the unit tests run during deployment.

On the default deploy.cmd file we have 2 steps:

  1. Build site (to a temporary path).
  2. Copy changed files from the temporary path to your wwwroot.

We'll add 2 steps in the middle like so:

1. Build site (to a temporary path).

2. Build test project (because the first step will only build whatever is required for the site and not other projects that may exist in the repository).

3. Run tests.

4. Copy changed files from the temporary path to your wwwroot (but only do this step if tests passed/build was successful).

To do that add the following code between step :: 1. and step :: 2. like so:

:: 1. Build to the temporary path
%MSBUILD_PATH% "%DEPLOYMENT_SOURCE%\MvcTest\MvcTest.csproj" /nologo /verbosity:m /t:Build /t:pipelinePreDeployCopyAllFilesToOneFolder /p:_PackageTempDir="%DEPLOYMENT_TEMP%";AutoParameterizationWebConfigConnectionStrings=false;Configuration=Release /p:SolutionDir="%DEPLOYMENT_SOURCE%\.\\" %SCM_BUILD_ARGS%
IF !ERRORLEVEL! NEQ 0 goto error

:: 2. Building test project
echo Building test project
"%MSBUILD_PATH%" "%DEPLOYMENT_SOURCE%\MvcTest.Tests\MvcTest.Tests.csproj"
IF !ERRORLEVEL! NEQ 0 goto error

:: 3. Running tests
echo Running tests
vstest.console.exe "%DEPLOYMENT_SOURCE%\MvcTest.Tests\bin\Debug\MvcTest.Tests.dll"
IF !ERRORLEVEL! NEQ 0 goto error

:: 4. KuduSync
call %KUDU_SYNC_CMD% -v 50 -f "%DEPLOYMENT_TEMP%" -t "%DEPLOYMENT_TARGET%" -n "%NEXT_MANIFEST_PATH%" -p "%PREVIOUS_MANIFEST_PATH%" -i ".git;.hg;.deployment;deploy.cmd"
IF !ERRORLEVEL! NEQ 0 goto error

As you can see the test runner being used is vstest.console.exe which is new for VS 2012, if you have VS2012 you can run this script locally (in a VS2012 command shell) to make sure your updated script works.

vstest.console.exe will recognize any test that exists in the supplied test assembly and will run it whether the test is mstest, xunit or nunit, and they could all be in the same assembly.

For a full working example of this capability try the following GitHub repository (which you can deploy on a test site just to get the feel of it):

Read more...

Simple terminal for your Azure Web App

This guide will help you run server-side commands on your Azure Web App hosting environment, it is very simple and useful and since it's implemented in node.js, it'll work on any (node) supporting OS (Windows / MAC / Linux).

  • First, you should install node.js if you don't already have it (#whynot?).

  • Install KuduExec:

    npm install kuduexec -g

    Note: There's also a .NET version of kuduexec called KuduExec.NET

  • Find your "kuduexec" Azure Web App endpoint:

    • Add "scm" after your site's name (if you have a custom domain you still need to add this to the original URL you received from Azure), for example:

      http://somesitename.azurewebsites.net/ --> https://somesitename.scm.azurewebsites.net/

    • If you have "git deployment" enabled on your site you can get the endpoint (including user name and password) from the Azure portal go to you site, under the CONFIGURE tab, on the git section in the DEPLOYMENT TRIGGER URL:

  • Now that you have your endpoint, open a shell window and run the command:

    kuduexec <Your endpoint URL / git deployment url>

    For example:

    kuduexec https://somesitename.scm.azurewebsites.net/

    Note: You can also add your user name and password to the url (otherwise it simply asks you for them):

    kuduexec https://username@somesitename.scm.azurewebsites.net/

  • At this point you'll see a command prompt with the root directory of your site (not "wwwroot"), some info on the directory structure.

  • Now you can run your commands, including shell commands such as "cd", "dir" and "copy", you'll also be able to run executables such as "git" and "npm".

  • To quit simply type exit

Usage Examples

  • One useful scenario this can help you with is when you want to run a garbage collection on your git repository, which can reduce your storage usage:

    • Simply go to the repository directory: cd site\repository

    • And run: git gc

  • Another will be to check if you some lingering processes using: ps -W

    • Here I have a lingering node.exe process and I kill it using: kill

      C:\DWASFiles\Sites\somesitename\VirtualDirectory0> ps -W
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
          14620       0       0      14620    ?    0 08:26:30 D:\Windows\SysWOW64\inetsrv\w3wp.exe
            824       0       0        824    ?    0 08:40:04 D:\Windows\SysWOW64\cmd.exe
          28484       0       0      28484    ?    0 08:40:04 D:\Program Files (x86)\nodejs\node.exe
          11584       1   11584      11584    ?  500 08:40:48 /bin/ps
      C:\DWASFiles\Sites\somesitename\VirtualDirectory0> kill -f 28484
      C:\DWASFiles\Sites\somesitename\VirtualDirectory0> ps -W
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
          14620       0       0      14620    ?    0 08:26:30 D:\Windows\SysWOW64\inetsrv\w3wp.exe
          29008       0       0      29008    ?    0 08:41:08 D:\Windows\SysWOW64\cmd.exe
          19756       1   19756      19756    ?  500 08:41:08 /bin/ps
      C:\DWASFiles\Sites\somesitename\VirtualDirectory0>
      

Important notes

  • This simple terminal is not for running interactive commands, running a command that requires user input will hang (for 3 minutes until recognized as hanging process and then it will be aborted) since there is no way, currently, to provide input for the running command.

  • The output for a single command will arrive only after the command finish running, so if you run a long running command it'll take time for the output to show (other than piping: requireInput.exe < input.txt).

Update

You can find an online terminal to your site under https://somesitename.scm.azurewebsites.net/DebugConsole/

Read more...