2

I have an Azure Devops build YAML file that contains the following task:

  - task: PowerShell@2
    displayName: 'Set coverage variable'
    inputs:
      targetType: 'inline'
      script: Write-Host "##vso[task.setvariable variable=coverage]Get-Content .\coverage\lcov-report\summary.txt"

I would like this task to set the variable coverage to be the value of the contents of summary.txt. However, when accessing the variable in the following task, I see that the variable is literally the string Get-Content .\coverage\lcov-report\summary.txt.

There seems to be no example anywhere in the documentation as to how to pull this off. Is it just a limitation of scripting? Do I have to set the Get-Content command to a variable and then access that variable?

2 Answers 2

5

Please use $() after ].

Example:

##vso[task.setvariable variable=coverage]$(Get-Content .\coverage\lcov-report\summary.txt)"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I was looking for
1

Actually, it's documented. But may be not too obvious in our official doc :) You could find related doc here:

Important

Predefined variables that contain file paths are translated to the appropriate styling (Windows style C:\foo\ versus Unix style /foo/) based on agent host type and shell type. If you are running bash script tasks on Windows, you should use the environment variable method for accessing these variables rather than the pipeline variable method to ensure you have the correct file path styling.

Since you are using is pipeline variable, the value returned by 'Get-Content .\coverage\lcov-report\summary.txt' as a string type, it's an expected behavior.

However $() accepts a variable not a string. Use $(Get-Content .\coverage\lcov-report\summary.txt)

After this, in the following task, simple use below to read the value.

Write-Host No problem reading $env:coverage 

1 Comment

Oh I see! I think there was just a mismatch in semantics for what was in my mind vs on the page. When I read that $() accepts a variable, I was imagining $(coverage) being what was required. I treated the $(Get-Content ...) as an expression instead of a variable

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.