TFS CI构建 - 在构建成功后更新自定义定义变量

Nil*_*iya 5 msbuild tfs continuous-integration azure-pipelines azure-artifacts

我们已经设置了我的TFS CI Build,我们正在管理一个变量用于维护版本控制,我们希望在每次成功构建后更新,任何想法如何操作?

我写过PowerShell脚本

param([Int32]$currentPatchVersion)
Write-Host "Current patch version "$currentPatchVersion
$NewVersion=$currentPatchVersion + 1
Write-Host "New patch version "$NewVersion
Write-Host ("##vso[task.setvariable variable=PackageVersion.Patch;]$NewVersion")
Run Code Online (Sandbox Code Playgroud)

但它只是在飞行中应用.

我希望将其应用于永久设置.

Edd*_*SFT 8

"##vso[task.setvariable variable=PackageVersion.Patch;]$NewVersion"只需在构建过程中设置变量值,它不会在构建定义级别设置值.如果要永久更新构建定义中的变量值,可以调用Rest API来设置定义中变量的值.有关详细信息,请参阅以下部分

创建一个"testvariable"例子: 在此输入图像描述

使用以下代码创建Power Shell脚本并将其上载到源代码管理中:

[String]$buildID = "$env:BUILD_BUILDID"
[String]$project = "$env:SYSTEM_TEAMPROJECT"
[String]$projecturi = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"

$username="alternativeusername"
$password="alternativepassword"

$basicAuth= ("{0}:{1}"-f $username,$password)
$basicAuth=[System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth=[System.Convert]::ToBase64String($basicAuth)
$headers= @{Authorization=("Basic {0}"-f $basicAuth)}

$buildurl= $projecturi + $project + "/_apis/build/builds/" + $buildID + "?api-version=2.0"

$getbuild = Invoke-RestMethod -Uri $buildurl -headers $headers -Method Get |select definition

$definitionid = $getbuild.definition.id

$defurl = $projecturi + $project + "/_apis/build/definitions/" + $definitionid + "?api-version=2.0"

$definition = Invoke-RestMethod -Uri $defurl -headers $headers -Method Get

$definition.variables.testvariable.value = "1.0.0.1"

$json = @($definition) | ConvertTo-Json  -Depth 999

$updatedef = Invoke-RestMethod  -Uri $defurl -headers $headers -Method Put -Body $json -ContentType "application/json; charset=utf-8"
Run Code Online (Sandbox Code Playgroud)

此脚本将获取当前构建定义并更新"testvariable"to 的值"1.0.0.1".您需要启用备用凭据.

然后,您可以在构建定义中添加"PowerShell脚本"任务来运行此脚本.