在脚本中动态设置Azure DevOps变量 [英] Dynamically set Azure DevOps variable in scripts

查看:95
本文介绍了在脚本中动态设置Azure DevOps变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 n 个变量,我需要在Release管道中将它们分配为Azure DevOps变量,而且看起来语法没有正确.

I have n number of variables that I need to assign as Azure DevOps variables in a Release pipeline, and it doesn't seem like I'm getting the syntax right.

变量可能具有不同的值(变量名),使得它们可以是:-{guid 1}-{guid 2}...

The variables may have different values (variable names) such that they could be: - {guid 1} - {guid 2} ...

所以在运行时之前我不会知道它们.问题是,似乎vso [task.setvariable]的所有示例都使用静态变量名,但是我需要动态设置它.

So I won't know them prior to runtime. The problem is that it seems all of the examples of vso[task.setvariable] use static variable names, but I need to set it dynamically.

这是应该起作用的,但不起作用:

Here's what should work but doesn't:

 Write-Host "##vso[task.setvariable variable=$($myVariable)]$($myValue)"

我还尝试过仅使用[Environment] :: SetEnvironmentVariable(与用户一起使用),并且它似乎不能在同一作业中的两个不同任务中持续存在.

I've also tried just using [Environment]::SetEnvironmentVariable (with user) and it doesn't seem to persist across two different tasks in the same job.

[Environment]::SetEnvironmentVariable($myVariable, $myValue, 'User')

(在后续任务中为空)

我是否可以通过某种方式动态创建在任务之间持续存在的发布变量?我尝试搜索,并在开发人员社区中找到一个问题,但没有答案.

Is there some way that I can dynamically create release variables that persist between tasks? I've tried to search and found one question on the developer community but no answer to it.

推荐答案

实际上看起来问题不是不是未设置变量,而是在使用task.setvariable之后,该变量仅在后续版本中可用任务(而不是当前任务).

It actually looks like the issue isn't that the variable isn't set, but that after using task.setvariable, the variable will only be available in subsequent tasks (and not the current one).

所以我想说这是在Azure DevOps中设置变量的最佳方法:

So I would say this is the best way to set variables in Azure DevOps:

当需要在同一任务/脚本步骤中使用变量时,请使用:

When needing to use variables in the same task/script step, use:

[Environment]::SetEnvironmentVariable(...)

或者仅在PowerShell中使用变量.

Or just use a variable in PowerShell.

当需要通过多个步骤使用变量时,请使用:

When needing to use variables with multiple steps, use:

$myVariable = "some name"
$myValue = "some value"

# Note that passing in $($variableName) should work with this call
Write-Host "##vso[task.setvariable variable=$($myVariable)]$($myValue)"

# Note that trying to use a Write-Host for $env:myVariable will return empty except in tasks after this one
Write-Host "Setting $($myVariable) to $($myValue)

这篇关于在脚本中动态设置Azure DevOps变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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