Azure ADO Pipeline Update Variables Value

Viewed 32

I'm not sure if this is possible but I wanted to know if there is a way in an ADO pipeline to update a pipeline level variable from within a poweshell step within the script and have that value persist.

For example I have a pipeline variable called count which is set to 0, under certain circumstances I need to increment count by 1 and then persist that value in the pipeline variable so it increments for each build when the conditions are met. Is this possible as I've not been able to find any documentation about this. I have tried this with the counter variable but it doesn't fit my needs as it increments for each build.

Below is a very cut down version of what I have

trigger:
- develop

pool:
  vmImage: 'windows-2019'

steps:

- task: PowerShell@2
  displayName: 'SetVariable'
  inputs:
    targetType: 'inline'
    script: |
      $(count) = $(count) + 1
1 Answers

You can install Build Updating Tasks extension, and use the Set variable on a build definition task to update a variable in a Build Definition with conditions.

For example:

  1. Create a variable in your pipeline (count here):

enter image description here

  1. Then, add the Set variable on a build definition task in your pipeline: enter image description here

Set the conditions based on your scenario, in the following YAML sample we set a variable doThing and the value is Yes. Set the condition in Set variable on a build definition task, if the value is Yes, then the count variable will Autoincrement.

steps:

# This step creates a new pipeline variable: doThing. This variable will be available to subsquent steps.
- bash: |
    echo "##vso[task.setvariable variable=doThing]Yes"
  displayName: Step 1

- task: BuildVariableTask@2
  displayName: Update the variable
  inputs:
    buildmode: 'Prime'
    variable: 'count'
    mode: 'Autoincrement'
    usePSCore: False
  condition: and(succeeded(), eq(variables['doThing'], 'Yes')) 
Related