Azure PowerShell and ARM template deployment from GitHub actions
Tags: github, azure, powershell, arm
Published: 2019-08-28 19:35:04

Earlier this month [GitHub Actions support for CI/CD was announced](https://github.blog/2019-08-08-github-actions-now-supports-ci-cd/). After that I have been thinking that this is something I need to learn more. So I decided to check how easy it would be to add Azure PowerShell support (which I can then use for my ARM template deployments). There was already az cli action available: 
{% github azure/actions %} 

But I wanted to replicate similar setup that I've used in Azure DevOps for many years. Therefore I decided to try to create my own action (and at the same time of course learn how these actions work). 

I created GitHub repository for my actions: 
{% github JanneMattila/actions %} I looked  the az cli repository so that I would better understand the implementation details of their action. I decided to reuse their login instructions so that you can easily jump between az cli and Azure PowerShell. After learning the setup from az cli repository I decided to use a bit simpler setup. It means that I didn't use the components that are provided in the [GitHub Action Toolkit](https://github.com/actions/toolkit) but instead I just implemented simple 45 line solution that consist only 3 files. But clearly toolkit repository is the place to go when you're doing some serious development for your actions. 

When I had my Azure PowerShell action ready I then created repository for demo application: 
{% github JanneMattila/AzurePwshARMActionDemo %}

It has my favorite setup from Azure infrastructure point of view:
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/2f0ef7phnoe7wd12t24e.png)

That of course means `deploy` folder and `deploy.ps1` and `azuredeploy*.json` files. This demo doesn't (yet?) have any application code in it but for example there are already actions to manage web apps so it would be easy thing to add:
{% github Azure/appservice-actions %}

Those files under `deploy` folder are exactly the same kind of ones I have been talking and presenting last years. Next step is to create new workflow that uses my previously created Azure PowerShell action. Here's example [worklow](https://github.com/JanneMattila/AzurePwshARMActionDemo/blob/master/.github/workflows/main.yml):
```yml
name: Azure Deployment
on: [push]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - uses: jannemattila/actions/azurepowershell@master
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}
        
    - name: Azure PowerShell & ARM template deployment
      run: ./deploy/deploy.ps1 -ResourceGroupName "pwsh-dev-rg" -Location "North Europe"
      shell: pwsh
```

Important part is `jannemattila/actions/azurepowershell@master` which means that I now reuse the action in my other repository in this workflow. So now it's suddenly super easy to user Azure PowerShell in your actions. That's the last step in my above workflow.

Remember that you need to have the Azure credentials correctly set before the workflow can even work:
![Azure credentials managed in secrets](https://thepracticaldev.s3.amazonaws.com/i/k51isnxwjhd2ppn06nyl.png)

Above workflow starts automatically based on event: `on: [push]` (in workflow definition). I can also view my worklow executions under Actions:

![Workflow runs visible under Actions menu](https://thepracticaldev.s3.amazonaws.com/i/eo282thqzhwv7xku4b5q.png)

You can further drill-down to the details of each run: 
![Workflow execution log](https://thepracticaldev.s3.amazonaws.com/i/p4flh6y0nbpef6jkq4hw.png)

And since I'm using ARM template deployments inside my PowerShell file I can see my deployments in Azure Portal as well:
![Resource group deployments in Azure Portal](https://thepracticaldev.s3.amazonaws.com/i/o003umhsdsp3m3c7n9uq.png)
That weird looking deployment name is coming from `GITHUB_SHA` environment variable which maps to the commit SHA in that repository. It means that you can fully backtrack to the actual change and investigate what has happened in detail:
```batch
[master ≡] # git checkout 933307a62df143ccfebab4aadd5925af22484d9c -b investigate-dev
Switched to a new branch 'investigate-dev'
[investigate-dev] # git log
commit 933307a62df143ccfebab4aadd5925af22484d9c (HEAD -> investigate-dev)
Author: Janne Mattila
Date:   Wed Aug 28 20:37:24 2019 +0300
...
```
I hope you find this post interesting!

**P.S.** If you thought that I managed to do this in one go... well no 😊:![Trial & error implementation](https://thepracticaldev.s3.amazonaws.com/i/iq7h22b0faemqy14d1l9.png)