Search code examples
azure-pipelinesazure-cli

azure Pipeline help: Run az cli stmt and evaluate the response as part of an IF block


I am creating an azure yaml pipeline and within an AzureClI@2 task I want to run the cli stmt 'az group exists...' with a local variable as the name and evaluate the statement's response, something like this:

 if ( az group exists -n $(dev-resource-group) = false)
 then
  # do something super cool here.
 elseif
  # do something less cool else.
 fi

There are lots of examples for variables but I'm unable to get this to work executing a cli stmt. I'm thinking that this needs to be runtime, so ${{ }} will probably be required. Any help here is appreciated!


Solution

  • I do agree with @Daniel Mann you need to use Script Type as Shell, Refer below:-

    enter image description here

    I tried the below code with if and else block in AzureCLI@2 to check if my resource group exist and it worked successfully like below:-

    By setting the $y to true:-

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: 'subscripption'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          x="rgname"
          y=$(az group exists --name $x --output json)
          
          if [ "$y" = "true" ]; then
              echo "Hello Rithwik Resource group exist"
          else
              echo "Hello Rithwik Resource group does not exist"
          fi
    
    

    Output:-

    enter image description here

    By setting the $y to false:-

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: 'subscription'
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |
          x="rgname2"
          y=$(az group exists --name $x --output json)
          
          if [ "$y" = "false" ]; then
              echo "Hello Rithwik Resource group exist"
          else
              echo "Hello Rithwik Resource group does not exist"
          fi
    
    

    Output:-

    enter image description here