I used to be able to use the service principal for AZ CLI or PWSH tasks in my DevOps pipeline, but have been banging my head against the wall since yesterday because I can’t get it to work anymore. Has something changed, or do I overlook something?
After reading this post I replicated the setup in my own pipeline, but the ‘servicePrincipalKey’ is empty.
The code I’m using in my pipeline is:
- task: AzureCLI@2
displayName: 'Get Azure CLI credentials'
inputs:
azureSubscription: $(serviceConnection)
scriptType: 'pscore'
scriptLocation: inlineScript
addSpnToEnvironment: true
useGlobalConfig: true
inlineScript: |
$InformationPreference = "Continue"
Write-Information "##vso[task.setvariable variable=CLIENT_ID]$env:servicePrincipalId" | Out-Null
Write-Information "##vso[task.setvariable variable=CLIENT_SECRET]$env:servicePrincipalKey" | Out-Null
if ([string]::IsNullOrEmpty($env:servicePrincipalKey)) {
Write-Warning "Service principal key is empty"
}
elseif ([string]::IsNullOrEmpty($env:servicePrincipalId)) {
Write-Warning "Service principal id is empty"
}
else {
Write-Output "Service principal id and key are set"
}
- task: Powershell@2
displayName: Output SPN
inputs:
targetType: inline
workingDirectory: '$(System.DefaultWorkingDirectory)'
script: |
Write-Host "CLIENT_ID: '$env:CLIENT_ID')"
Write-Host "CLIENT_SECRET: '$env:CLIENT_SECRET'"
The results are:
AzureCLI@2 task:
WARNING: Service principal key is empty
Powershell@2 task:
CLIENT_ID: ‘***’)
CLIENT_SECRET: ”
Any thoughts?