Search code examples
f#environment-variablesf#-fakeappveyorgitversion

How to use GitVersion Environment variables


I have a project which I used to build via AppVeyor. The build sequence was the following:

  1. Install and run GitVersion
  2. Build project
  3. Create package using evaluated version number.

The last step was done by the PowerShell command:

nuget pack path/tofile.nuspec -Version (get-item env:GitVersion_InformationalVersion).Value

As you can see the version is taken from environment variable defined by GitVersion. Now I want to migrate the build to a FAKE build script.

I have these dependencies defined in my script.

"Clean"
  =?> ("GitVersion", Choco.IsAvailable)
  ==> "RestorePackages"
  ==> "BuildApp"
  ==> "CreatePackage"
  ==> "Default"

Git version step is straightforward.

Target "GitVersion" (fun _ ->
    "gitversion.portable" |> Choco.Install id
    Shell.Exec("gitversion","/l console /output buildserver" ) |> ignore
)

I can see in my logs that variables are set by GitVersion.

Adding Environment Variable. name='GitVersion_SemVer' value='1.1.1-xxx'

The next step is to create the package.

Target "CreatePackage" (fun _ ->
    TraceEnvironmentVariables()
    let version = 
        match buildServer with 
        | AppVeyor -> environVar "GitVersion_SemVer"
        | _ ->  baseVersion + "-local"
    NuGet (fun p -> 
        {p with
            OutputPath = packagingDir
            WorkingDir = "."
            Version = version
            Publish = false }) 
            nuspecFileName
)

I'm printing all the variables defined, after that I'm trying to get the version by reading variable and assigning it to version.

Unfortunately version stays empty when I run the build. After I added TraceEnvironmentVariables() method call I can see that none of the variables defined by GitVersion is presented in the output.

As John Palmer and dustinmoris said the process started by Shell.Execute sets all variables as process-level ones.

Is there a way to use Shell.Execute so that the process can set global scope environment variables?

UPD

As a workaround, I've added extra step in AppVeyor.yml config file:

init:
  - git config --global core.autocrlf input
install: 
  - choco install gitversion.portable -y
before_build:
  - ps: gitversion /l console /output buildserver /b (get-item env:APPVEYOR_REPO_BRANCH).Value 

build_script:
  - cmd: build.bat BuildApp

In this case, variables are set in a global scope and I can get them and use in my build script.

Obviously, PowerShell starts GitVersion in a different way. I guess, I should mimic it somehow in my build script.

So my question remains the same, how to use GitVersion as a target in my script and get the version number back.


Solution

  • Have you had a look at Fake GitVersionHelper? http://fsharp.github.io/FAKE/apidocs/fake-gitversionhelper.html

    #r "packages/FAKE/tools/FakeLib.dll"
    open Fake
    open Fake.GitVersionHelper
    
    let version = GitVersion (id)
    
    printfn "FullSemVer %s" version.FullSemVer
    printfn "NuGetVersionV2 %s" version.NuGetVersionV2