Deploy Azure App Service via Azure CLI

Azure CLI (or Bash) is a cross-platform command-line interface with commands to create and manage Azure resources. It’s an easy-to-use to use, cross-platform alternative for Powershell.
Further reading: docs.microsoft.com

For me, the reason to look at Azure CLI, was an upgrade of Powershell. The deployment scripts I was using no longer worked, so I either had to rewrite my Powershell scripts or move to Bash. I chose the latter. One further note. I started working for the client in 2016. That’s the reason why we don’t use Azure DevOps and YAML pipelines. Those technologies were not mature at the time. Moreover, Azure DevOps pipelines are not exactly easy to use.

Anyhow. Here is the script to deploy an Azure App Service. Note that I manually create a deployment package (*.zip) in Visual Studio first. Also, we don’t use service principals, which would be an obvious improvement. Less relevant by the way, since we run the deployment scripts by hand and not unattended. Suboptimal indeed, I know.

#!/bin/bash

if [ “$1” == “Dev” ]; then
AppServiceName=DevService
ResourceGroup=appservices-dev
ZipFile=Service.zip
ParameterFile=@ParametersDev.json
echo “Start Deploy $AppServiceName”
fi

az login -u <username> -p <password>

# AppService Deploy
az webapp deployment source config-zip –resource-group $ResourceGroup –name $AppServiceName –src $ZipFile

# Add ApplicationSettings
az webapp config appsettings set -g $ResourceGroup -n $AppServiceName –settings $ParameterFile

We cannot run bash scripts from the command line just like that. I used Visual Studio Code, but I’m sure there are alternatives.

  • Open Visual Studio Code
  • Select Bash as the default Shell
    Menu View / Command Pallette / Terminal: Select Default Shell / Git Bash
  • If the Terminal Window is not open yet:
    View / Terminal
  • Run Command from Terminal window (wait for prompt, may take a while):
    Bash DeployDevService.sh Dev (after change directory/cd)

Using AzureCLI in DevOps

To create a table and perform a set of inserts, you can use the Azure CLI action in a DevOps Release pipeline. Reference the script and pass your arguments, like so:

StorageTableCreate.cmd:

call az storage table create –name %1 –account-name %2

call az storage entity insert –table-name %1 –account-name %2 –entity PartitionKey=[systema] RowKey=system Active=true Active@odata.type=Edm.Boolean LastRevision=1000000 LastRevision@odata.type=Edm.Int32 –if-exists fail

call az storage entity insert –table-name %1 –account-name %2 –entity PartitionKey=[systemb] RowKey=system Active=true Active@odata.type=Edm.Boolean LastRevision=1000000 LastRevision@odata.type=Edm.Int32 –if-exists fail & exit 0

Note that the first statement doesn’t end with ‘exit 0’. This means that the next insert statement will be processed, regardless of the result of the first insert. This means: a record will be added if a record for that system doesn’t exist yet.