Get Env Variable set in Azure Powershell in Dev Ops Pipeline

我只是一个虾纸丫 提交于 2021-02-15 07:43:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!