Adding a custom deployment script to ignore web test projects in Azure Functions with GitHub deployment

The scenario

As part of an existing Azure Functions app that has some HTTP Functions that generate Excel files and return them as a byte array, I wanted to add an integration tests web project that when run would download the resulting Excel files. This Azure Functions app is automatically deployed from a GitHub repo.

Kudu and deployment scripts

When using GitHub to deploy any Azure App Service app (including Azure Function apps), Kudu chooses which deployment script to use based on your project type. As it states in the Customizing deployments article in the Kudu wiki, if you happen to have a web project in the repository, then it chooses to build and deploy that project.

So, what happens if you add a web project to the repo where you have your Azure Functions? Well, when Kudu gets the new bits, it finds the web project, builds and deploys it, and your Functions are now gone! But don’t worry, there’s a very simple solution for this: using a custom deployment script.

Generating the deployment script

To generate the custom deployment script we will start with the standard deployment script Kudu uses to deploy Azure Function apps and then we will modify it to ignore our test project folder.

So, how do we get that script?

One easy way is to download it from our Kudu site:

  • Go to Kudu. e.g. https://{site}.scm.azurewebsites.net/
  • From the Tools menu, choose ‘Download deployment script’
  • Put the files in the root of your repo

But, if we’ve already pushed our web project, the deployment script we’ll download is the one for web applications. In that case we can use a tool called kuduscript.

Let’s first install the tool:

  1. We need node.js installed.
  2. Then we will install kuduscript with npm:
    npm install kuduscript -g

After that, we run it in the root of our repo on the Windows Command line (if you try running it on Linux/macOS you will get an error because there is no bash template available):

kuduscript -y --functionApp

This will generate two files:

  • .deployment: contains the command to run for deploying your app
  • deploy.cmd: contains the deployment script

Modifying the deployment script for the project

Now we need to tell Kudu to ignore the folder that contains our test project (In my case I added the project in a folder called tests).

In the deploy.cmd file, there are two lines that start with call :ExecuteCmd "%KUDU_SYNC_CMD%" ... (lines 85 and 98 in my case). This command has an “ignore” (-i) parameter at the end, telling it which files/folders to ignore when copying the files to the destination folder. That’s where we need to add our folder to the list.

For example, we turn this:

call :ExecuteCmd "%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;node_modules"

Into this:

call :ExecuteCmd "%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;node_modules;tests"

All that remains now is to commit these two files to our repo and push the changes.

@gjbellmann

Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s