Search code examples
azure-devopsazure-pipelinesazure-powershellazure-pipelines-yamlazure-yaml-pipelines

How to change the value of a YAML variable between tasks inside the same job


How to set a new value of a YAML variable in YAML file through a task, and the subsequent task will display the new value using powershell? (both tasks are in the same job)

I am trying with below code, but its not working. The second/subsequent task STILL gets the initial value even when the first task set a new value to that YAML variable

stages:
  - stage: tst
    displayName: tst_stage

    jobs:
      - deployment: 
        displayName: 'test 1'
        environment: 'test 1'

        variables:
        - name: someName
          value: "someValue"

First task that will set/update the value of the variable :

- task: PowerShell@2
    name: "task1"
    displayName: this is task1
    inputs:

      targetType: 'inline'
      script: |
        $newValue = "ThisIsNewValue"
        echo "##vso[task.setvariable variable=someName;isOutput=true]$newValue"

Second task will display the variable with new value :

- task: PowerShell@2
    name: "task2"
    displayName: this is task2
    inputs:

      targetType: 'inline'
      script: |
        echo "the new value of the variable is : $(someName)"

Now the expected output of the task 2 should be :

the new value of the variable is : ThisIsNewValue

But for some reason the actual output that I got is the initial value :

the new value of the variable is : someValue

Solution

  • change this line of code from :

    echo "##vso[task.setvariable variable=someName;isOutput=true]$newValue"
    

    to :

    Write-Host "##vso[task.setvariable variable=someName;]$newValue"