Search code examples
azure-devopscicdazure-pipelines-yaml

The variable "component" in the azure pipeline shown below is not getting updated?


Altough result of ("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") is true The variable "component" in the azure pipeline shown below is not getting updated?

variables:
  newrel: "0.4.7"
  component: ""
trigger: none
pr: none
pool:
  name: CC-DEV
resources:
  pipelines:
    - pipeline: CCM_LV_Pipeline
      source: HYBM_CCM_LV
      project: CC-Wolf
      trigger:
        branches:
          include:
            - '*'
jobs:
  - job: DetermineComponentDirectory
    displayName: "Determine Component Directory"
    steps:
      - checkout: none
      - powershell: |
          if ("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
              Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
              Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)\assets\ccm-lv"
          }
          else {
              Write-Host "No successful pipeline found in CC-Wolf. Using default component directory."
              Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)\assets\default"
          }
          Write-Host "Component directory set to: $(component)"
        displayName: "Set Component Directory"
    ```

Solution

  • As clarified in your another case, the following command line is to create an output variable that is different from the exiting general variable, even if they have the same name (component). They have different expressions to access their values.

    Write-Output "##vso[task.setvariable variable=component;isOutput=true]xxxx"
    

    In addition, on the PowerShell task which calls the 'setvariable' command to set/update the variable, the new value is not available for current PowerShell task. The new value only available for the subsequent tasks.


    If you just want to update the value of the existing general variable (component) and only want use the new value in the current job (DetermineComponentDirectory), you should use the command like as below.

    variables:
      newrel: "0.4.7"
      component: "$(Build.SourcesDirectory)/assets/default"  # The default component directory.
    
    resources:
      pipelines:
      . . .
    
    jobs:
    - job: DetermineComponentDirectory
      displayName: "Determine Component Directory"
      steps:
      - task: PowerShell@2
        displayName: "Set Component Directory"
        inputs:
          targetType: inline
          script: |
            if("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
              Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
              Write-Output "##vso[task.setvariable variable=component;]$(Build.SourcesDirectory)/assets/ccm-lv"
            }
            else {
              Write-Host "No successful pipeline found in CC-Wolf. Using the default component directory."
            }
      
      - task: PowerShell@2
        displayName: 'Print component directory'
        inputs:
          targetType: inline
          script: Write-Host "Component directory set to $(component)"
    

    If you want to also pass the updated value to the subsequent jobs or stages within the same pipeline, you can persist setting it as an output variable. About output variables, you can see "Use output variables from tasks".

    variables:
      newrel: "0.4.7"
    
    resources:
      pipelines:
      . . .
    
    stages:
    - stage: A
      jobs:
      - job: DetermineComponentDirectory
        displayName: "Determine Component Directory"
        steps:
        - task: PowerShell@2
          displayName: 'Set Component Directory'
          name: setCompDir
          inputs:
            targetType: inline
            script: |
              if("$(Resources.TriggeringAlias)" -eq "CCM_LV_Pipeline") {
                Write-Host "CCM_LV_Pipeline in CC-Wolf succeeded"
                Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)/assets/ccm-lv"
              }
              else {
                Write-Host "No successful pipeline found in CC-Wolf. Using the default component directory."
                Write-Output "##vso[task.setvariable variable=component;isOutput=true]$(Build.SourcesDirectory)/assets/default"
              }
        
        - task: PowerShell@2
          displayName: 'Print component directory'
          inputs:
            targetType: inline
            script: Write-Host "Component directory set to $(setCompDir.component)"
      
      # Pass the output variable to another job within the same stage.
      - job: A2
        dependsOn: DetermineComponentDirectory  # Must dependsOn the previous job which generates the output variable.
        variables:
          ComponentDirectory: $[ dependencies.DetermineComponentDirectory.outputs['setCompDir.component'] ]
        steps:
        - task: PowerShell@2
          displayName: 'Print component directory'
          inputs:
            targetType: inline
            script: Write-Host "The Component directory is $(ComponentDirectory)"
    
    # Pass the output variable to another stage within the same pipeline.
    - stage: B
      dependsOn: A  # Must dependsOn the previous stage which generates the output variable.
      variables:
        ComponentDirectory: stageDependencies.A.DetermineComponentDirectory.outputs['setCompDir.component']
      jobs:
      - job: B1
        steps:
        - task: PowerShell@2
          displayName: 'Print component directory'
          inputs:
            targetType: inline
            script: Write-Host "The Component directory is $(ComponentDirectory)"