Skip to content

Instantly share code, notes, and snippets.

@IlyaFinkelshteyn
Last active July 13, 2017 22:17
Show Gist options
  • Save IlyaFinkelshteyn/cf03fce8c7c7d343542c4a29b850b046 to your computer and use it in GitHub Desktop.
Save IlyaFinkelshteyn/cf03fce8c7c7d343542c4a29b850b046 to your computer and use it in GitHub Desktop.
$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