Manage AppService slots with Azure YAML pipelines

Lucas Calje
Level Up Coding
Published in
3 min readApr 27, 2020

--

YAML pipelines are the new way to build CI/CD pipelines in Azure DevOps. Although they don’t have that nice GUI which the classical pipelines have, this is totally compensated by the fact that these YAML pipelines are part of your source code and can be shared easily.

In this post, I will be using YAML pipelines to demonstrate how AppService slots can be created dynamically based on branch names and how to cleanup slots for branches that are merged back to master.

Create an AppService slot

The first step is to create the slot, but to be able to do that, we need to know what the name of the slot should be. So the first step of the first step is to extract that name from the branch name. In my case I use the following naming convention

<type>/<scope>-<keyword/description>

in which scope is a unique number and an ideal candidate for naming slots. Luckily, Azure makes our lives easy by providing a lot of information to pipelines, like, for example, the last segment of a branch name: Build.SourceBranchName. With this information, it is not very hard to extract the name using a bash task.

Template which extracts the slot name from the branch name

Note that the##vso tag is used to put the extracted slot name into a variable to make it available for our pipeline. Later on in this article we have to extract the name of the slot again, not from the branch name but from the commit message instead. So before we continue, I’ll already rewrite this snippet into a reusable Azure template

Determine slot name in an Azure template

This template requires two parameters, input is the string with the name of slot in it, and slotRegexp is the regular expression which can extract that name. This template can be used as follows

Pipeline snippet using the extract-slot-name template

Finally, with an Azure CLI task the slot can be created as follows

Create AppService slot

The tricky part here is probably how I reference the slot name. The way it works is to add a dependency on the job which extracts the slot name (dependsOn: setup), and to make the slot name available again (slotName). Important to note as well, is the subtle difference in how variables are referenced in those example snippets, like $(..) ${{..}} $[..], which is explained in more detail here.

Remove AppService slot

The scenario to cleanup slots occurs when you merge a branch back to master. However, when you squash all commits and merge, all information about the source branch is lost inside the pipeline. So this time we need to focus on that merge commit message. I follow the semantic commit messages guidelines, so when I merge I write messages like

test(1234): improve test quality

This is more or less identical to what we did in the previous section, except we have to change the regular expression a bit and use Build.SourceVersionMessage instead.

The slot can now be removed again with an Azure CLI task. Below I have this task written as a template with the slot name as an input parameter:

Pipelines

So far I’ve only written about things which of course are part of a bigger process, the whole pipeline. That subject is way too big for one article, but for those of you who are interested how everything fits together I’ll add my two pipelines here anyway:

Branch deployment to slot pipeline:

Branch deployment pipeline

Master deployment and slot cleanup pipeline:

Master deployment pipeline

--

--