问题
I have 2 Azure Dev Ops Pipeline Tasks within a pipeline - the first calls a powershell script that does some stuff and then sets an Env variable I want to reference again;
$env:MYVALUE = "ABC"
This powershell script is executed and works well - however I thought I would be able to reference $MYVALUE in the pipeline after it running;
task: SqlAzureDacpacDeployment@1
displayName: 'Do The thing'
inputs:
...
DatabaseName: '$(MYVALUE)'
...
Is it possible to obtain the ENV variable from the pipeline?
回答1:
The sample you shared is to create process variable, so it is lost when the process exits and you can't access the variable from another process (PowerShell instance).
We should set the variable via the power script Write-Host "##vso[task.setvariable variable={variable name}]{variable value}", then we can call the variable in the another task.
A skeleton version looks like this:
pool:
vmImage: 'ubuntu-latest'
trigger:
- none
steps:
- powershell: |
Write-Host ("##vso[task.setvariable variable=MYVALUE]ABC")
displayName: 'PowerShell Script'
- powershell: |
Write-Host "The value of MYVALUE is : $($env:MYVALUE)"
displayName: 'PowerShell Script'
来源:https://stackoverflow.com/questions/63250174/get-env-variable-set-in-azure-powershell-in-dev-ops-pipeline