简体   繁体   中英

How can I set an Azure Devops pipeline variable via an inline PowerShell command?

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?

Please use $() after ].

Example:

##vso[task.setvariable variable=coverage]$(Get-Content .\coverage\lcov-report\summary.txt)"

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM