【问题标题】:Update build variable using powershell in tfs在 tfs 中使用 powershell 更新构建变量
【发布时间】:2017-10-10 18:01:34
【问题描述】:

我正在使用 Visual Studio TFS 服务并想运行一个 powershell,更新一个变量并将其用于另一个任务。

我的构建定义只有 2 个 powershell 任务和 1 个变量 (NugetEnabled),默认值为“dontpush”。

构建变量:NugetEnabled(值:“dontpush”)

任务 1:Powershell 脚本

##vso[task.setvariable variable=NugetEnabled;]push

任务 2:Powershell 脚本

$value= $env:NugetEnabled

if ($value)
{
    Write-Output "Value of the nuget is set and equals to $value"
}
else
{
    Write-Output "Value of the nuget doesn't exists."
}

输出控制台写道:

"Value of the nuget is set and equals to dontpush"

预期值:“推”。

我无法更新该值。我想更新值,因为我想使用“自定义条件”来推送我的 nuget 包。

完整的powershell脚本is hosted in github和我的代理版本是2.116.1

提前致谢! 任何建议都会受到赞赏和有用。

【问题讨论】:

  • 我用Write-Output ("##vso[task.setvariable variable=NugetEnabled;]push") 更改了命令并且完美运行!...

标签: powershell tfs continuous-integration


【解决方案1】:

事实上,我正在使用 TFS 2017 和库来执行 powershell vsts 任务:

github.com/Microsoft/vsts-task-lib/tree/master/powershell

因此,使用构建/发布任务之间共享的变量变得非常简单。

示例(导入 ps 模块):

$Script_Path = $PSScriptRoot
Import-Module $script_Path"\VstsTaskSdk\VstsTaskSdk.psd1" -force #2>&1> $null

$Root_Path =  $ENV:ROOT_PATH
$Repo_Path = $ENV:BUILD_SOURCESDIRECTORY
$Repo_Branch = $ENV:BUILD_SOURCEBRANCHNAME
$Repo_CID = $ENV:BUILD_CID
$Log_Folder = $ENV:LOG_FOLDER
$SCRIPT_DB = $ENV:SCRIPT_DB
$Build_result_folder = $ENV:BUILD_RESULT_FOLDER



$Artifact_Path=$ENV:BUILD_ARTIFACTSTAGINGDIRECTORY
write-host "Artifact_Path : "$($Artifact_Path)  -backgroundcolor "Red"

$File_Log = $Log_Folder+"\LOG-TFS-"+$ENV:BUILD_BUILDNUMBER+".log"
Write-Host "Fichier de log : "$FileLog -backgroundcolor "Red"
Set-VstsTaskVariable -Name "LOG_FILE" -Value $File_Log  #2>&1> $null

设置变量值:Set-VstsTaskVariable

获取变量值:Get-VstsTaskVariable

获取变量值,也可以使用TFS自动导出的环境变量,在ps脚本中使用时内置变量的命名规则会发生变化,就是名称转换为“.”。到“_”。

示例:$ENV:BUILD_SOURCEBRANCHNAME 在 TFS UI 界面中变为 Build.SourceNameBranche。 否则,在 TFS 中定义自己的变量并在 PS 中使用它作为我的示例,$ENV:... 无需命名转换。

构建变量详细信息:https://www.visualstudio.com/en-us/docs/build/define/variables

发布变量详细信息:https://www.visualstudio.com/en-us/docs/build/concepts/definitions/release/variables

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 2019-12-22
    • 2020-12-30
    • 2013-05-20
    • 2018-03-06
    • 2017-12-16
    • 1970-01-01
    • 2015-12-21
    • 2019-11-04
    相关资源
    最近更新 更多