Hello @Siegfried Heintze !
Allow me to try to answer one by one !
1.Yes, you use the PowerShell Script Action to run PowerShell scripts directly in your GitHub workflow.
2.You can use the Azure CLI directly in GitHub workflows. To run Azure CLI commands, first, you'd set up Azure login with a step like this, using the Azure Login action:
- name: Login to Azure
uses: azure/login@v1
with:
**creds: ${{ secrets.AZURE_CREDENTIALS }}**
And here is an example :
name: Azure Workflow
on:
push:
branches:
- main
jobs:
azure_operations:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Run Azure CLI command
- name: List Azure Resource Groups using Azure CLI
run: az group list --output table
# Run PowerShell command with Azure PowerShell Module
- name: Get Azure VMs using PowerShell
run: |
Install-Module -Name Az -AllowClobber -Scope CurrentUser -Force
Import-Module Az
$rgs = az group list --query "[].name" --output tsv
foreach ($rg in $rgs) {
$vms = Get-AzVM -ResourceGroupName $rg
$vms | ForEach-Object { Write-Output $_.Name }
}
3.Creating a Service Principal and storing it in GitHub Secrets is the right approach. Once you've created the Service Principal with az ad sp create-for-rbac
, you'll have the necessary permissions to make calls to the Azure Resource Manager (ARM). The ROLE is the permission bound to the Principal , it could be any role available on Azure IAM , usually the Contributor is set , but best practices instruct us to use the least privilege approach !
Additionally you can use Azure PowerShell to make REST calls with Invoke-RestMethod
.
$uri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$rg/providers/Microsoft.Web/sites/$functionApp/functions/$functionName/listkeys?api-version=2015-08-01"
$functionKeys = Invoke-RestMethod -Method Post -Uri $uri -Headers @{ Authorization = "Bearer $accessToken" }
Note: $accessToken
should be the token you get after logging in to Azure with PowerShell.
- In PowerShell, you don't need
jq
since it has native support for JSON. Here's how you can translate the Azure CLI command:
$uri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$rg/providers/Microsoft.Web/sites/$functionApp/functions?api-version=2015-08-01"
$functions = Invoke-RestMethod -Method Get -Uri $uri -Headers @{ Authorization = "Bearer $accessToken" }
$functionNames = $functions.value.properties.name
I hope this helps!
Kindly mark the answer as Accepted and Upvote in case it helped!
Regards