Jay
Jay

Reputation: 319

How to set and retrieve variable value for condition in YAML Pipelines?

The condition on "Deploy_Infrastructure" is using the variable isInfraCreated. The value of isInfraCreated keeps returning 'unknown', even though the PowerShell script sets the values. What am I doing wrong here?

trigger:
- main

stages:
- stage: development
  variables:
    isInfraCreated: "unknown"
    aksClusterName: "bla-aks"
    aksResourceGroup: "bla-aks-resources-01"
  jobs:
  - job: Prepare
    steps:
    - task: AzureCLI@2
      displayName: 'Azure PowerShell: Determine isInfraCreated'
      inputs:
        azureSubscription: 'myAzureSubscription'
        scriptType: 'pscore'
        scriptLocation: 'inlineScript'
        inlineScript: |
          $aksResource = az aks show --name $(aksClusterName) --resource-group $(aksResourceGroup)
          $aksResource
          if ($aksResource -eq $null)
          {
            Write-Host "##vso[task.setvariable variable=isInfraCreated]$false"
            $isInfraCreated = $false
            $env:isInfraCreated = $false
          }
          else 
          {
            Write-Host "##vso[task.setvariable variable=isInfraCreated]$true"
            $isInfraCreated = $true
            $env:isInfraCreated = $true
          }
  - job: Deploy_AKS_Infrastructure 
    condition: and(succeeded(), eq(variables.isInfraCreated, 'False'))

Upvotes: 0

Views: 3620

Answers (2)

4hbane
4hbane

Reputation: 187

I am not sure if this is the problem but I believe that the syntax for condition of the last job should be like this :

- job: Deploy_AKS_Infrastructure 
  condition: and(succeeded(), eq(variables.isInfraCreated, false))

Upvotes: 2

Leo Liu
Leo Liu

Reputation: 76660

How to set and retrieve variable value for condition in YAML Pipelines?

To resolve this issue, we need to share variables across jobs by using output variables from tasks:

  • To reference a variable from a different task within the same job, use TASK.VARIABLE.
  • To reference a variable from a task from a different job, use dependencies.JOB.outputs['TASK.VARIABLE'].

So, we need update the scripts to:

trigger:
- main

stages:
- stage: development
  variables:
    isInfraCreated: "unknown"
    aksClusterName: "bla-aks"
    aksResourceGroup: "bla-aks-resources-01"
  jobs:
  - job: Prepare
    steps:
    - task: AzureCLI@2
      displayName: 'Azure PowerShell: Determine isInfraCreated'
      inputs:
        azureSubscription: 'myAzureSubscription'
        scriptType: 'pscore'
        scriptLocation: 'inlineScript'
        inlineScript: |
          $aksResource = az aks show --name $(aksClusterName) --resource-group $(aksResourceGroup)
          $aksResource
          if ($aksResource -eq $null)
          {
            Write-Host "##vso[task.setvariable variable=isInfraCreated;isOutput=true]$false"
            $isInfraCreated = $false
            $env:isInfraCreated = $false
          }
          else 
          {
            Write-Host "##vso[task.setvariable variable=isInfraCreated;isOutput=true]$true"
            $isInfraCreated = $true
            $env:isInfraCreated = $true
          }
      name: SetIsInfraCreated

  - job: Deploy_AKS_Infrastructure 
    variables:
      TestisInfraCreated: $[ dependencies.Prepare.outputs['SetIsInfraCreated.isInfraCreated'] ]
    condition: and(succeeded(), eq(variables.TestisInfraCreated, 'False')) 

You could check the document How to pass variables in Azure Pipelines YAML tasks for some more details.

Upvotes: 1

Related Questions