Change build version prior all calls

BlackGad's Avatar

BlackGad

11 Jul, 2017 10:12 PM

I am changing build version on init script. But system goes crazy if i trying to reset counter (it is part of version format)

Build started
$parent_info = Invoke-RestMethod -Uri "https://ci.appveyor.com/api/projects/BlackGad/ps-build"
$match = [regex]::match($parent_info.build.version, "(\d+).(\d+)(-.+)?")
if($match.Success) 
{ 
    $major = $match.Groups[1].Value
    $minor = $match.Groups[2].Value
    $newVersion = $major + "." + $minor + "." + $env:APPVEYOR_BUILD_VERSION
    Update-AppveyorBuild -Version $newVersion
}
else
{
    throw [System.ArgumentException] "Invalid parent build version"
}
Exception calling "UpdateBuildDetails" with "10" argument(s): "Build with specified version already exists."
At C:\Program Files\AppVeyor\BuildAgent\Modules\build-worker-api\build-worker-api.psm1:41 char:5
+     [Appveyor.BuildAgent.Api.RestBuildServices]::UpdateBuildDetails($ ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Exception
It seems that system rechecking version number uniq before actually change in method Update-AppveyorBuild.
Btw can i generate garbage in version pattern? some kind of noise in pattern?
  1. Support Staff 1 Posted by Ilya Finkelshte... on 11 Jul, 2017 10:41 PM

    Ilya Finkelshteyn's Avatar

    It is difficult to understand what happens without full configuration, especially without version format for the project. If it is standard like 1.2.{build}, then for build number 5 it will set its version to 1.2.1.2.5. Is it what intended?

    Please send full configuration in YAML format and also clarify what is the goal of this script.

  2. 2 Posted by BlackGad on 12 Jul, 2017 06:46 AM

    BlackGad 's Avatar

    there is 2 builds parent and child

    parent has version pattern MAJOR.{parentCounter}
    child has version pattern {childPattern}

    on child init script i fetch latest parent build number, filter it from pull request part, concatenate with child automatically generated child version ({childCounter} and optional pull request part)

    After EVERY parent build I reset child counter to 1

    Example step by step compilation flow:
    1) Parent build version 1.2
    2) Child version become 1.2.1
    3) Child version become 1.2.2
    4) 3) Child version become 1.2.3
    5) Parent build version 1.3
    6) Child version become 1.3.1
    7) Child version become 1.3.2

    So appveyor system before child init script has version 1, 2, 3 etc
    After Init my script called which modifies current child build version with Update-AppveyorBuild function to unique (1.2.1, 1.2.2, 1.3.1). But there is defect in Update-AppveyorBuild. It is trying to recheck current version unique before applying.

  3. 3 Posted by BlackGad on 12 Jul, 2017 08:17 PM

    BlackGad's Avatar

    Finally seems I solve this flow. Many thanks to your for this sample.

    Script below require 3 env variables to be defined:
    API_TOKEN - encrypted variable with your API token
    MASTER_ACCOUNT_NAME - parent build account name
    MASTER_PROJECT_SLUG - parent build slug

    1) This script will try to fetch and parse parent build version from history (MAJOR and MINOR).
    2) Then script will try to fetch and parse current build version.
    3) Set current build number to some unique nonce (next steps could throw exceptions on secure api calls)
    4) Compare parent build version and current. If parent is higher current build number will be reset to 1 and set next build number 2
    5) Finally based on parent MAJOR.MINOR numbers, current BUILD_NUMBER and optional pull request nonce current build version will be set

    Environment->Init script

    $token = $env:API_TOKEN
    $masterAccountName = $env:MASTER_ACCOUNT_NAME
    $masterProjectSlug = $env:MASTER_PROJECT_SLUG
    
    $prevVersion = [version]::Parse("0.0")
    $ciApiURL = "https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG"
    $ciHistory = Invoke-RestMethod -Uri "$ciApiURL/history?recordsNumber=2" -Method Get
    if ($ciHistory.builds.Count -eq 2)
    {
        $prevVersionMatch = [regex]::match($ciHistory.builds[1].version, "(\d+)\.(\d+)(-.+)?")
        if($prevVersionMatch.Success) 
        {
            $prevVersion = [version]::Parse($prevVersionMatch.Groups[1].Value + "." + $prevVersionMatch.Groups[2].Value);
        }
    }
    
    $nonceVersion = $prevVersion.ToString(2) + "." + $env:APPVEYOR_BUILD_NUMBER + "-" + [guid]::NewGuid().ToString("N")
    Update-AppveyorBuild -Version "$nonceVersion"
    
    $masterVersion = [version]::Parse("0.0")
    $masterApiURL = "https://ci.appveyor.com/api/projects/$masterAccountName/$masterProjectSlug"
    $masterHistory = Invoke-RestMethod -Uri "$masterApiURL/history?recordsNumber=1" -Method Get
    if ($masterHistory.builds.Count -eq 1)
    {
        $masterVersionMatch = [regex]::match($masterHistory.builds[0].version, "(\d+)\.(\d+)(-.+)?")
        if($masterVersionMatch.Success) 
        {
            $masterVersion = [version]::Parse($masterVersionMatch.Groups[1].Value + "." + $masterVersionMatch.Groups[2].Value);
        }
    }
    
    if($masterVersion -gt $prevVersion)
    {
        Write-Warning "Master build version was increased. Resetting build number to 1."
            
        #reset current build number to 1 and next one to 2
        $env:APPVEYOR_BUILD_NUMBER = "1"
    
        $headers = @{
          "Authorization" = "Bearer $token"
          "Content-type" = "application/json"
        }
    
        $body = @{ nextBuildNumber="2" }
        $body = $body | ConvertTo-Json
        Invoke-RestMethod -Uri "$ciApiURL/settings/build-number" -Headers $headers -Body $body  -Method Put
    }
    
    $newVersion = $masterVersion.ToString(2) + "." + $env:APPVEYOR_BUILD_NUMBER
    if(![string]::IsNullOrEmpty($env:APPVEYOR_PULL_REQUEST_NUMBER))
    {
        $newVersion = $newVersion + "-" + $env:APPVEYOR_PULL_REQUEST_NUMBER
    }
    
    Write-Host("New version is: " + $newVersion)
    
    Update-AppveyorBuild -Version "$newVersion"
    
  4. Support Staff 4 Posted by Ilya Finkelshte... on 13 Jul, 2017 10:32 PM

    Ilya Finkelshteyn's Avatar

    I reviewed your script and sample, and honestly I do not like my approach I made in that sample. Specifically I do not like that whole version is being reset all the time, so each build starts with default version, and then being reset to new. Possibility of conflicts still exist and also it is not good for GitHub status during the build.

    I changed sample to update version format when needed, and current version only when needed too. Please check those lines.

  5. Support Staff 5 Posted by Ilya Finkelshte... on 13 Jul, 2017 10:45 PM

    Ilya Finkelshteyn's Avatar

    Note that this requires that version is removed from YAML configuration file, to honor what is set in UI. As I made in my tests here. This behavior is specific for version, not every YAML setting.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

24 Jul, 2017 09:09 PM
24 Jul, 2017 12:39 PM
23 Jul, 2017 12:53 AM
22 Jul, 2017 08:14 PM
22 Jul, 2017 07:59 PM