简体   繁体   English

TFS版本管理中的自定义变量

[英]Custom variables in TFS Release Management

I'm using TFS 2017.1 Builds and Release feature. 我正在使用TFS 2017.1构建和发布功能。 In my release definition, I have a couple of release variables which I need to refer in my PowerShell task (execute on remote machine). 在我的发布定义中,我有几个发布变量,需要在PowerShell任务中引用(在远程计算机上执行)。 So far, I've tried the following options but couldn't get it to work. 到目前为止,我已经尝试了以下选项,但无法使其正常工作。

Added an Execute PowerShell task to store release variables into Environment variables: 添加了Execute PowerShell任务以将发布变量存储到环境变量中:

$releaseVaraiables = Get-ChildItem Env: | Where-Object { $_.Name -like "ACL_*" } 
Write-Host "##vso[task.setvariable variable=aclVariables]$releaseVaraiables"

Added an Execute PowerShell on remote machine task: 添加了在远程计算机上执行Execute PowerShell任务:

Here I can't read the Environment variables (maybe because this is remote machine task?) 在这里,我无法读取环境变量(也许因为这是远程计算机任务?)

Write-Verbose "problem reading $env:aclVariables" -Verbose

Then I tried passing the environment variable as an argument, but that didn't work either 然后我尝试将环境变量作为参数传递,但是那也不起作用

param
(
    $RbacAccessTokenParams
)

$RbacAccessTokenParams.GetEnumerator() | % {$_.Name}
$RbacAccessTokenParams | % {
    Write-Verbose "variable is $_" -Verbose
    Write-Verbose "name is $_.Name" -Verbose
    Write-Verbose "value is $_.Value" -Verbose
}

This is how I passed as argument 这就是我作为论点传递的方式

-RbacAccessTokenParams $(aclVariables)

What I'm missing here? 我在这里想念的是什么?

Non-secret variables are already stored as environment variables; 非秘密变量已经存储为环境变量; you do not need to do anything special to access them. 您无需执行任何特殊操作即可访问它们。 You can access them with $ENV:VariableName . 您可以使用$ENV:VariableName访问它们。 Periods are replaced with underscores. 下划线将替换为下划线。 So Foo.Bar would be $env:FOO_BAR . 所以Foo.Bar将是$env:FOO_BAR

Secret variables should be passed in to the script that requires them. 机密变量应传递到需要它们的脚本中。

However, this only applies on the agent . 但是,这仅适用于代理 If you're using the PowerShell On Target Machines task to run a script, you need to pass the variables as arguments to the script. 如果您使用“ PowerShell On Target Machines上运行PowerShell On Target Machines任务来运行脚本,则需要将变量作为参数传递给脚本。 There is no way around this, unless you choose to use deployment groups. 除非您选择使用部署组,否则无法解决此问题。

Or, better yet, follow a configuration-as-code convention and store application-specific values in source controlled configuration files that your scripts read, so that you are not tightly coupled to your deployment orchestration platform. 或者,更好的是,遵循配置即编码的约定,并将特定于应用程序的值存储在脚本读取的源代码控制的配置文件中,以使您不会与部署流程平台紧密耦合。

I've tested your scenario on my side with TFS 2017.3.1, and it works when pass the environment variable as an argument. 我已经使用TFS 2017.3.1在我这边测试了您的方案,并且在将环境变量作为参数传递时可以使用。 You can upgrade your TFS first and try again. 您可以先升级TFS,然后重试。 Attach my steps for your reference: 附上我的步骤供您参考:

1. 1。 在此处输入图片说明

2. 2。 在此处输入图片说明

3. 3。

在此处输入图片说明

    4. 4。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM