1

I am trying to run an Azure DevOps Task with nested az cli command. I have defined the variables at the beginning of the azure-pipelines.yaml

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  resourceGroup: test1234
  acrName: taerarawrr2.azurecr.io

Within tasks I can access the values by using $(resourceGroup) and $(acrName). Now I want to run an az cli task which uses these two variables, and saves the output of the command in a new variable (ACR_LOGIN_SERVER), which can be later used in the task.

- task: AzureCLI@2
  displayName: Run docker image
  inputs:
    azureSubscription: serviceprincipaltest
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      $ACR_LOGIN_SERVER= $(az acr show --name $(acrName) --resource-group $(resourceGroup) --query "loginServer" --output tsv)
      echo $(ACR_LOGIN_SERVER)

This however fails for some reason. I guess there is something wrong with the syntax of this nested command. I've tried using parentheses, $ and quotation marks in various locations but nothing seems to work. I also didn't manage to find any example how such an inline script should be formatted correctly. Any help would be greatly appreciated.

2 Answers2

1

Here is the link of Azure CLI task.

You have $() around az acr command, which will try to find a variable named like that, which is not there. Pls remove that and use the script like below:

call {your command}>tmpFile1
set /p myvar= < tmpFile1 
echo "##vso[task.setvariable variable=testvar;]%myvar%"

OR

FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
SET var=%%F
)
echo "##vso[task.setvariable variable=testvar;]%var%"

Check out this thread: Set Output Variable in Azure CLI task on VSTS

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
0

There are two problems with your script

  • The command substituion using $(command) does not work in Azure DevOps as it tries to replace that with a variable. use the backtick notation Instead: `command`
  • When assigning a variable in bash, you should not prefix the name with $, that is for referencing.
ACR_LOGIN_SERVER=`az acr show --name $(acrName) --resource-group $(resourceGroup) --query "loginServer" --output tsv`

echo $ACR_LOGIN_SERVER
danielorn
  • 5,254
  • 1
  • 18
  • 31