Azure PowerShell and ARM template deployment from GitHub actions
(Source/Credits: https://dev.to/janne_mattila/azure-powershell-and-arm-template-deployment-from-github-actions-2038)
Earlier this month GitHub Actions support for CI/CD was announced. After that I have been thinking th...
Earlier this month GitHub Actions support for CI/CD was announced. 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 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:
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:
```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:
Above workflow starts automatically based on event: on: [push]
(in workflow definition). I can also view my worklow executions under Actions:
You can further drill-down to the details of each run:
And since I'm using ARM template deployments inside my PowerShell file I can see my deployments in Azure Portal as well:
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 😊:
Comments section