Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$token = $env:API_TOKEN #should be defined as a secure variable
$headers = @{
"Authorization" = "Bearer $token"
"Content-type" = "application/json"
}
$apiURL = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
$history = Invoke-RestMethod -Uri "$apiURL/history?recordsNumber=2" -Headers $headers -Method Get
$pomVersion = ([xml](Get-Content .\pom.xml)).project.version
if ($history.builds.Count -eq 2)
{
$previousVersion = $history.builds[1].version
$previousMajor = $previousVersion.Substring(0, $previousVersion.LastIndexOf("."))
Write-Host "Previous major version: $previousMajor"
Write-Host "Current version from pom.xml: $pomVersion"
if ($previousMajor -ne $pomVersion)
{
Write-Warning "Major version has been changed, resetting build number and version format"
$versionFormat="$pomVersion.{build}"
Write-Warning "New build version format: $versionFormat. Please ensure that it is not set in YAML"
$s = Invoke-RestMethod -Uri "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/settings" -Headers $headers -Method Get
$s.settings.versionFormat = $versionFormat
$s.settings.nextBuildNumber = "2"
Invoke-RestMethod -Uri 'https://ci.appveyor.com/api/projects' -Headers $headers -Body ($s.settings | ConvertTo-Json -Depth 10) -Method Put
#reset current build number to 1 and next one to 2
$env:APPVEYOR_BUILD_NUMBER = "1"
Update-AppveyorBuild -Version "$pomVersion.$env:APPVEYOR_BUILD_NUMBER"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment