【问题标题】:Refresh environment variables in Azure Pipeline build在 Azure Pipeline 构建中刷新环境变量
【发布时间】:2020-04-27 00:14:48
【问题描述】:

如何为间接更改这些变量的同一构建中的后续步骤刷新环境变量?

重现所描述行为的测试 YAML 文件部分。

jobs:
- job: welldone
  pool: 
    name: noodle
  steps:
  - script: |
      echo select TestStand 2016
      start /wait "" "C:\Program Files (x86)\National Instruments\Shared\TestStand Version Selector\TSVerSelect.exe" /version 16.0 /installing /noprompt
    displayName: 'select TestStand version 16'

  - script: |
      echo Check TestStand version
      echo %TestStand%
      call RefreshEnv.cmd
      echo %TestStand%
    displayName: 'print TestStand version'

  - script: |
      call checkTSversion.bat
      call RefreshEnv.cmd
      call checkTSversion.bat
    displayName: 'call bat file to print TestStand version'

第一个脚本调用 TestStand Version Selector 更改环境变量等。

第二个脚本打印以“teststand”开头的环境变量,然后调用refreshenv.cmd 并再次打印变量。首先打印旧变量,第二个 - 更新。我想这与 cmd 的预期行为一致。

第三个脚本做同样的事情,但现在echo %TestStand% 在一个单独的批处理文件中。它的行为与第二个脚本完全相同。

我可以在第一个脚本中做些什么来确保连续的脚本会读取更新的环境变量?

【问题讨论】:

  • 你不能。每次启动 cmd 的新实例并更改环境变量时,它都不会将这些更改传递给父 cmd。
  • @DavidPostill 感谢您的评论。看起来这是我问题的答案(即“你无能为力,不可能”)。我可以更新当前脚本的变量。有更好的方法吗?完全不同的方法,也许?那我应该问另一个措辞不同的问题吗?

标签: cmd environment-variables yaml azure-pipelines teststand


【解决方案1】:

我不完全确定我了解您的示例管道,但在我看来,您想在作业步骤中设置变量和/或更改变量值,然后在以后的作业步骤中使用新值。正确的?如果是这样,您正在寻找这样的东西:

steps:
# Create a variable
# Note that this does _not_ update the environment of the current script.
- bash: |
    echo "##vso[task.setvariable variable=sauce]crushed tomatoes"

# An environment variable called `SAUCE` has been added to all downstream steps
- bash: |
    echo "my environment variable is $SAUCE"
- pwsh: |
    Write-Host "my environment variable is $env:SAUCE"

来源:https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 2021-12-19
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多