"##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"
此脚本将获取当前构建定义并更新"testvariable"
to的值"1.0.0.1"
。您需要启用替代凭据。
然后你可以在你的构建定义中添加一个“PowerShell Script”任务来运行这个脚本。