Imagine situation: you need to create build definition. You set some version like: 1.0.0.$(Rev:v)
After that tfs start create build with number 1.0.0.1; 1.0.0.2 and etc.
And you have task to clone this build and in some reason you need that build start from number 1.0.0.100.
What should you do? Of course first think which you will have it is how to set $(Rev:v). For the date, when I write this article it is impossible. Only open database and try to find the last version for this build.
And because it is not good way, and some of us haven't access to DB, I've prepare for you script which resolve this issue.
1. Create variables. Call it version.
2. Set the number from which you want to start the build.
3. Set Build number format to $(version). (Instead of 1.0.0.$(Rev:v))
4. Add script to first place.
5. Create variable Login and Password. You need set account who have access to modify build definition. Don't forget about crypt you password.
6. Map this script to $(build.sourcesDirectory)\Scripts or put content of script to Inline powershell.
Here content of script:
<#
.SYNOPSIS
This is script was writenn by Maxim Karyonov. Script update the number of build definition.
Use it, when you need start build from specific number.
And each build number should increase.
.DESCRIPTION
Version 1.0
This script possible put to VNext build and execute as external powershell script.
VNext build should put through build definition variables.
Create variable "version" in build definition.
Set Build number format to $(version).
You have to put to execute this script in first place.
It will update next build.
.EXAMPLE
Put script to $(build.sourcesDirectory)\Scripts\.
Create variables: Login and Password.
Script filename: $(build.sourcesDirectory)\Scripts\IncreaseNumberOfBuild.ps1
Arguments: -username $(Login) -password $(Password)
#>
Param(
$username,
$password
)
Write-Host "Start script to increase number"
[String]$buildID = "$env:BUILD_BUILDID"
[String]$project = "$env:SYSTEM_TEAMPROJECT"
[String]$projecturi = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
[String]$BuildNumber = "$env:BUILD_BUILDNUMBER"
Write-Host "Proceed login information. Build ID: $BuildNumber"
$buildNumberArr=$BuildNumber.Split('.')
$buildNumberArr[3]=[convert]::ToInt32($buildNumberArr[3])+1
$BuildNumber=$buildNumberArr[0]+"."+$buildNumberArr[1]+"."+$buildNumberArr[2]+"."+$buildNumberArr[3]
Write-Host "Create URl to get all builds"
$buildurl= $projecturi + $project + "/_apis/build/builds/" + $buildID + "?api-version=2.0"
Write-Host "Crypt password"
$secpassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secpassword)
Write-Host "Invoke URL to find build number $buildurl"
$getbuild = Invoke-RestMethod -Uri $buildurl -Credential $credentials -Method Get |select definition
$definitionid = $getbuild.definition.id
Write-Host "Definition ID: $definitionid"
$defurl = $projecturi + $project + "/_apis/build/definitions/" + $definitionid + "?api-version=2.0"
Write-Host "Get JSON of build. URL: $defurl"
$definition = Invoke-RestMethod -Uri $defurl -Credential $credentials -Method Get
Write-Host "Set the version of build. $BuildNumber"
$definition.variables.version.value = $BuildNumber
Write-Host "Save result"
$updatedef = Invoke-RestMethod -Uri $defurl -Credential $credentials -Method Put -Body (Convertto-Json $definition -Depth 99) -ContentType "application/json"
When you save build and execute it, my script will increase your number of build definition and save this value for future build.
This script has one minus: if start two build in one time, the script won't have time to change number of build. That's why if your build download artifact long time, or this build use very often for check-in or just trigger every second, I advice to configure with capability this build only for one agent.
Have a good day!
No comments:
Post a Comment