Search code examples
gitgithubappveyor

How to use the git branch as build nr in AppVeyor


In AppVeyor I would like to setup the build number with the release number from Github. That would be used for the AssemblyVersion patching.

I am using GitFlow, and have a release branch. In SourceTree I create a new Release, for example v1.2, which creates the branch release/v1.2. The 1.2 part I would like to use in AppVeyor.

Like, Build version format:{gitRelease}.{build}

Screenshot AppVeyor build nr

for this to work,

  1. I need to retrieve the gitHub branch name,
  2. Extract the release number from it,
  3. Put that in a variable {gitRelease}
  4. Run this before the assemblyVersion patching

But I cannot find how to do this.

For the build itself I have a build script, to sent the output (nuget package) to Octopus Deploy, so that means there is no Before build script section.


Solution

  • With the help from the Support Staff from AppVeyor, I got it working. 1733-how-to-call-the-assemblyversion-patch-from-the-build-script

    I have to use the init section in the appveyor.yml. And I used a seperate cmd line for the UpdateBuild - version (I had some trouble with the qoutes).

    init:
    - cmd: "set appVeyorBuildVersion=%appveyor_build_version%\necho appVeyorBuildVersion:%appVeyorBuildVersion% \n\nset branch=%APPVEYOR_REPO_BRANCH%\necho branch:%branch%\n\nset gitVersion=%branch:~-3%\necho gitversion:%gitVersion%\n\nset newVersion=%gitVersion%.%APPVEYOR_BUILD_NUMBER%\necho %newVersion%\n\n"
    - cmd: appveyor UpdateBuild -Version "%newVersion%"
    
    assembly_info:
      patch: true
      file: '**\AssemblyInfo.*'
      assembly_version: '{version}'
      assembly_file_version: '{version}'
      assembly_informational_version: '{version}'
    build_script:
    - cmd: "echo Building version:%appveyor_build_version%"
    - cmd: "nuget restore\nmsbuild MySolution.sln /t:build /p:Configuration=Release"
    

    The command line part (better readable):

    echo repo branch:%APPVEYOR_REPO_BRANCH%
    
    set branch=%APPVEYOR_REPO_BRANCH%
    echo branch:%branch%
    
    set gitVersion=%branch:~-4%
    echo gitversion:%gitVersion%
    
    
    set newVersion=%gitVersion%.%APPVEYOR_BUILD_NUMBER%
    echo %newVersion%
    
    appveyor UpdateBuild -Version "%newVersion%"