I'm having a bit of a struggle right now with the Azure Bash@3 task. My goal here is to see if a specific API connection exists in some resource group. To do so, I want to catch the length of the error message generated by the bash command that generates the someVar variable, where varLength is supposed to contain this length.
if it is 0, there was no error and the resource thus exists, if it is > 0, there is no error and the resource does not exist. I use this later for some Bicep trickery.
Now the problem I face is that the second task gets met with the error message: ERROR: Please run 'az login' to setup account.. Now I see a lot that you want to use the AzureCli@2 task for running az commands. However, this does not work for me, since using the command someVar=$(az resource show -g rg-logicapps-$env-001 -n sharepoint-connection-$env-1 --resource-type Microsoft.Web/connections 2>&1) in an AzureCLI script of type 'bash' will make the code error and make the task fail (since it generates an error). I cannot capture the error output in the same way as with the Bash@3 task here.
This makes the AzureCLI command not fit for my purposes. I was under the impression myself that running a simple AzureCLI task would enable me to use az commands in subsequent scripts, even ones that are of task Bash@3, since this would maybe keep the machine logged in. Is there any way to be able to run this command in the Bash@3 task, since the AzureCLI@2 task won't fit my specific needs here?
jobs:
- job:
steps: # A simple first login to be able to run the 'az' command in the second task
- task: AzureCLI@2
inputs:
azureSubscription: $(azureServiceConnection)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az --version
az account show
# Outputs a variable that is the same as the length of error message when searching for sharepoint connection. 0 if connection exists, > 0 if it does not exist
- task: Bash@3
inputs:
script: |
env=$1
someVar=$(az resource show -g rg-logicapps-$env-001 -n sharepoint-connection-$env-1 --resource-type Microsoft.Web/connections 2>&1)
varLength=$(echo -n "$someVar" | wc -m)
echo "$someVar"
echo "##vso[task.setvariable variable=sharepointConnExists;isOutput=true]$varLength"
arguments: '${{ variables.environment }}'
targetType: 'inline'
name: "DetermineConnExists"