在 Dev Ops Pipeline 的 Azure Powershell 中获取环境变量集 [英] Get Env Variable set in Azure Powershell in Dev Ops Pipeline

查看:28
本文介绍了在 Dev Ops Pipeline 的 Azure Powershell 中获取环境变量集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个管道中有 2 个 Azure Dev Ops 管道任务 - 第一个调用执行某些操作的 powershell 脚本,然后设置一个我想再次引用的 Env 变量;

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"

这个 powershell 脚本被执行并且运行良好 - 但是我想我可以在它运行后在管道中引用 $MYVALUE;

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)' 
      ...

是否可以从管道中获取 ENV 变量?

Is it possible to obtain the ENV variable from the pipeline?

推荐答案

你分享的示例是创建进程变量,所以当进程退出并且你不能从另一个进程访问该变量时它会丢失(PowerShell实例).

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).

我们应该通过power脚本Write-Host设置变量##vso[task.setvariable variable={variable name}]{variable value}",然后我们可以在另一个任务中调用该变量.

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.

骨架版本如下所示:

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'

这篇关于在 Dev Ops Pipeline 的 Azure Powershell 中获取环境变量集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆