Home > Enterprise >  Running powershell commands from Gitlab CI
Running powershell commands from Gitlab CI

Time:05-24

I am trying to execute the following yaml from a windows 10 machin that has been configured as runner with Powershell as the shell. I have installed Azure CLI and Azure powershell modules on the windows machine. When I execute the pipeline, it runs the Azure CLI commands however the Azure powershell commands were not recognized. I am able to run these commands locally on the windows machine. Any idea what is missing here ?

variables: DEFAULT_RG: description: "Default resource group to deploy the resources for testing" value: "newgrp" DEFAULT_LOCATION: description: "Default location of the testing resource group" value: "East US"

default: image: mcr.microsoft.com/azure-cli before_script: - az login --service-principal --username $SP_ID --password $SP_SECRET --tenant $TENANT_ID - az account set --subscription $SUBSCRIPTION_ID - set -euo pipefail

stages:

  • deploy deploy automation account and tie it with UAMI: stage: deploy

script: - New-AzAutomationAccount -Location $Location -Name $automationccount -ResourceGroupName $ResourceGroup - Set-AzAutomationAccount -ResourceGroupName $ResourceGroup -Name $automationccount -AssignUserIdentity "/subscriptions/$SUBSCRIPTION_ID/resourcegroups/$resourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$userAssignedOne"

CodePudding user response:

In your defaults, you're using mcr.microsoft.com/azure-cli which is a Linux-based image. If you need to run Azure PowerShell commands you need to specify a windows runner. Since you are not specifying what runner to use explicitly in your deploy automation account and tie it with UAMI job, it'll inherit from the default to use the Linux-based Docker image.

You can either use Azure CLI equivalent for creating new automation accounts:

or use a windows runner if you need to run the Azure PowerShell commands.

CodePudding user response:

Thanks for the response. Actually I removed the Azure-CLI image and tried a windows runner. I can confirm that it picked my windows runner where I had installed both Azure CLI and Azure powershell however the the runner couldn't recognize Azure Powershell commands. You may see the yaml below :

  stage: build_deploy
  script:
    - echo $SP_ID
    - echo $TENANT_ID
    - echo $SUBSCRIPTION_ID
    - az login --service-principal --username $SP_ID --password $SP_SECRET --tenant $TENANT_ID
    - az account set --subscription $SUBSCRIPTION_ID
    - set -euo pipefail
    - New-AzAutomationAccount -Location $Location -Name $automationccount -ResourceGroupName $ResourceGroup
    - Set-AzAutomationAccount -ResourceGroupName $ResourceGroup -Name $automationccount -AssignUserIdentity "/subscriptions/$SUBSCRIPTION_ID/resourcegroups/$resourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$userAssignedAcc"
  • Related