TFS CI构建-构建成功后更新自定义定义变量 [英] TFS CI Build - Update custom define variable after build is succeed

查看:125
本文介绍了TFS CI构建-构建成功后更新自定义定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经设置了TFS CI构建,并且出于维护版本控制的目的,我们正在管理一个变量, 我们想在每次成功构建后进行更新,知道如何做吗?

We had set up my TFS CI Build, and we're managing one variable for our purposes to maintaining versioning, We want to update after every success build, any idea how to do so?

我已经编写了PowerShell脚本

I had written PowerShell script

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")

但它只是即时应用.

我想将其永久应用于设置.

I want's to apply it on setting permanently.

推荐答案

"##vso[task.setvariable variable=PackageVersion.Patch;]$NewVersion"只是在构建过程中设置变量值,而没有在构建定义级别中设置值.如果要永久更新构建定义中的变量值,可以调用

"##vso[task.setvariable variable=PackageVersion.Patch;]$NewVersion" just set the variable value in the build process, it doesn't set the value in the build definition level. If you want to update the variable value in the build definition permanently, you can call the Rest API to set the value of the variables in the definition. Refer to following section for details:

创建一个"testvariable"作为示例:

Create a "testvariable" as example:

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

Create a Power Shell script with following code and upload it into source control:

[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"的值更新为"1.0.0.1".您需要启用备用凭据.

This script will get the current build definition and update the value of "testvariable" to "1.0.0.1". You need to enable alternative credential.

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

And then you can add a "PowerShell Script" task in your build definition to run this script.

这篇关于TFS CI构建-构建成功后更新自定义定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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