1

I have a Tests step in my pipeline. I want that step to run all tests on scheduled runs but only impacted Tests on manual or gated runs. To do this, I linked the runOnlyImpactedTests value to a process variable and I'm trying to modify its value using Powershell.

I'm able to read the variable but I can't change it. I have two questions but first, here are the PS scriptlet

Write-Host "a ${Env:Parameters_runOnlyImpactedTests} b"
Write-Host "##vso[task.setvariable variable=Parameters.runOnlyImpactedTests]True"
Write-Host "c ${Env:Parameters_runOnlyImpactedTests} d"

#Build Reasons:
#https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff736208(v=vs.120)

if($Env:BUILD_REASON -eq "Schedule"){
    Write-Host "Is Scheduled. Should run all tests"
    Write-Host "##vso[task.setvariable variable=Parameters.runOnlyImpactedTests]False"
}

#gci env:
#gci variable:

# Use the environment variables input below to pass secret variables to this script.

and its output

2019-11-04T18:32:37.5783531Z Génération du script.
2019-11-04T18:32:37.6377454Z a False b
2019-11-04T18:32:37.6408504Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\BuildsVnexAgents\Agt1-3348\_work\_temp\2fca26a4-3bb5-4d51-9d90-a29aa4c755b5.ps1'"
2019-11-04T18:32:37.9377454Z c False d
2019-11-04T18:32:37.9846229Z 

I also tried setting the variable using Parameters_runOnlyImpactedTests and Env:Parameters_runOnlyImpactedTests but they all give the same result.

My two questions now:

  1. Why isn't the variable taking the value
  2. If it can't work that way, are there alternatives in achieving my goal?
2
  • 1
    You may want to try line #3 in next step. According to the documentation the value gets initialized after the task is completed. learn.microsoft.com/en-us/azure/devops/pipelines/process/… Commented Nov 4, 2019 at 20:04
  • I was also working toward this information. What I found is that it works but not all the time. I tried using the exact same line in a script with comments at the top that failed and then without the comments and it worked. I event had to almost identical scripts, the only difference was in the YAML where the one not working had some spaces before the write-host command. But at least it Please write this as an answer so I can award you your points. Commented Nov 4, 2019 at 21:11

2 Answers 2

3

Does this works for you?

${Env:Parameters_runOnlyImpactedTests} = $false
Write-Host "a ${Env:Parameters_runOnlyImpactedTests} b"
${Env:Parameters_runOnlyImpactedTests} = $true
Write-Host "c ${Env:Parameters_runOnlyImpactedTests} d"

Output:

2019-11-05T09:56:39.8505031Z a False b
2019-11-05T09:56:39.8513437Z c True d

${Env:Parameters_runOnlyImpactedTests} should also avalible in other pipeline tasks.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and though some times in works (the value is changed in the next task), other times it doesn't. I also tried to change ProcParam_runOnlyImpactedTests and got the same result. In all cases, no matter if the value was shown has modified or not, the Tests task does not use the value. Maybe I should cancel the question?
It's hard to give you a answer to that. For my experiance my example works. Do you use Azure DevOps Service or Azure DevOps Server? Do use a Self-Hosted Agent? My be we can answer 'If it can't work that way, are there alternatives in achieving my goal?'.
0

Testing according to Fairoz's comment, it is true that when "##vso[task.setvariable variable=Parameters.runOnlyImpactedTests]True" is used in the script, the output of the value of this variable will not change in the same task. The modified value will be output normally in the subsequent task.

steps:  
    - powershell: 
        Write-Host " a $(Parameters_runOnlyImpactedTests) a "
        "##vso[task.setvariable variable=Parameters_runOnlyImpactedTests]True"
        Write-Host " b $(Parameters_runOnlyImpactedTests) b "
    - powershell: Write-Host "c $(Parameters_runOnlyImpactedTests) c "

enter image description hereenter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.