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.

1
  • Are the following replies helpful? Commented Feb 18, 2021 at 9:15

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.