Azure DevOps Recipe: Deploying Azure Logic App using Powershell Script
Tags: azure, devops, logicapps, powershell
Published: 2020-03-31 17:43:12

Azure Logic Apps is a somewhat unique cloud service that allows to connect your business-critical apps and services, automating your workflows without writing a single line of code.
There are numerous articles out there about how to deploy Azure Logic App with Azure Resource Manager templates, including official Microsoft documentation:
[Automate deployment for Azure Logic Apps by using Azure Resource Manager templates](https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-azure-resource-manager-templates-overview)
But in a corporate environment this kind of resources, especially in Production environment, are predefined and secured. Developers often do not have permission to overwrite existing resources, and deploying with ARM templates does exactly that. The template defines the infrastructure, resources, parameters, and other information for provisioning and deploying your logic app. But what we need is to leave the infrastructure alone and deploy just our logic app and its parameters.
So what to do when you dealing with this kind of restricted environment but still want to leverage the power of CI/CD pipelines?
Fortunately, when you create a Logic App using Visual Studio, a sample deployment Powershell script named "Deploy-AzureResourceGroup.ps1" is created for us too. In fact, it uses Azure PowerShell extension that simplifies the management of Azure Cloud services.
![Deploy-AzureResourceGroup.ps1](https://dev-to-uploads.s3.amazonaws.com/i/073uov0gbvwebqhm0vs1.png)

Here is a beginning of this script, pay attention to the parameters that we can provide to customize it:
```powershell
Param(
    [string] [Parameter(Mandatory=$true)] $ResourceGroupLocation,
    [string] $ResourceGroupName = '<YOUR RESOURCE GROUP NAME>',
    [switch] $UploadArtifacts,
    [string] $StorageAccountName,
    [string] $StorageContainerName = $ResourceGroupName.ToLowerInvariant() + '-stageartifacts',
    [string] $TemplateFile = 'LogicApp.json',
    [string] $TemplateParametersFile = 'LogicApp.parameters.json',
    [string] $ArtifactStagingDirectory = '.',
    [string] $DSCSourceFolder = 'DSC',
    [switch] $ValidateOnly
)
...
```
A full code for the deployment script is provided at the end of the article for those who want to use VS Code or other editor to develop a Logic App.
Let's leverage this script and create CI/CD pipelines in Azure DevOps.

##Creating a build artifact in Azure DevOps pipeline
The only things that we need to create a deployable artifact are the files that are located in the project's folder: a JSON file containing the workflow, a parameters file, and a script. So a build pipeline could be extremely simple, consisting just of one step, like the one below.
```yaml
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: LogicApp'
  inputs:
    PathtoPublish: 'src/LOGIC_APP_FOLDER'
    ArtifactName: 'LogicApp'
    publishLocation: 'Container'
```
Execute the build pipeline to create an artifact.

##Release pipeline
We can start with an empty Release pipeline, and add an artifact produced by the Build pipeline.
![Release pipeline](https://dev-to-uploads.s3.amazonaws.com/i/utnegdwqrergaz99tywd.png)
Next, we add a stage, and a single Azure Powershell Task. It is very important to select a _Task Version_ as "2.\*" and, correspondingly, *Azure PowerShell Version* as "Specify Other Version" and *Preferred Azure PowerShell Version* as "2.1.0" as that is the version that the deployment script is using.
![Azure Powershell task](https://dev-to-uploads.s3.amazonaws.com/i/igr6h6xe3fi5jjvgj47p.png)
Other important parameters are _Script Path_ - you can navigate to your artifact and select a deployment script.
Also, *Script Arguments* is a field where we can provide parameters to the script itself. Those arguments correspond to the parameters that we see at the beginning of the script. 
A good thing about Azure Powershell task is that it will automatically authenticate it to the Azure via service connection, so you do not have to worry about that.

## Powershell Script
Here is a gist with a complete script's code:
{% gist https://gist.github.com/i-b1/dd11f313dcacb0cf0c4fef28dc9fbf6e %}

##Conclusion
An alternative method to deploying Azure Logic app is to use Azure Powershell script that is relatively straightforward, does not require elevated permissions, and is applicable in the enterprise and restricted environments.

 Well, that is all for this simple recipe. Good luck, and when developing your Logic App be sure not to use a __cat logic__!
![Cat Logic](https://dev-to-uploads.s3.amazonaws.com/i/43iubneyzcru5zx3skgz.jpg)
<sup><sub>Photo credit: [Ryozuo](https://www.reddit.com/r/AnimalsBeingDerps/comments/a6yufu/these_cats/)</sub></sup>