Is it possible to use environment variables in dotnet_csproj section

jstafford's Avatar

jstafford

14 Feb, 2018 10:37 PM

I'm trying to better manage version numbers between the build itself and the assembly info in my dotnet core apps. In order to do so, I was attempting to set an environment variable for the application version, reset the version (using that application version) of the build using Update-AppveyorBuild, and then use that same variable in the patching process for csproj. It came out something like this:

dotnet_csproj:
    patch: true
    file: '**\*.csproj'
    version: '$env:app_version'
    package_version: '$env:app_version.{build}'  
    file_version: '$env:app_version.{build}'
    assembly_version: '1.0.0'

But it doesn't seem to be working as I thought it would. I'm not seeing any errors (or the result, for that matter) in the logs, but the builds are failing with several errors that I can only assume are cascading from this (the build was working before this change, so it's not the project), and that it is a result of the assembly patching not succeeding or outputting something invalid:

C:\Users\appveyor\AppData\Local\Temp\1\NuGetScratch\4tbo4y1o.zlv.nugetrestore.targets(131,5): error MSB4018: The "WriteRestoreGraphTask" task failed unexpectedly.

So is it possible to use environment variables in the csproj patching section and how?

  1. 1 Posted by Ilya Finkelshte... on 14 Feb, 2018 10:42 PM

    Ilya Finkelshteyn's Avatar

    You use PowerShell notation. We support the following notations right now:

    $(variable) - MSBuild-like variables
    %variable% - batch-like variables
    ${variable} - Shell-like variables
    
  2. 2 Posted by jstafford on 15 Feb, 2018 12:23 AM

    jstafford's Avatar

    So it would be like this, then?

    dotnet_csproj:
        patch: true
        file: '**\*.csproj'
        version: '${app_version}'
        package_version: '${app_version}.{build}'  
        file_version: '${app_version}.{build}'
        assembly_version: '1.0.0'
    

    This seems to exhibit the same failure (not sure if coincedence. I still can't see what those values are resolving to

  3. 3 Posted by Ilya Finkelshte... on 15 Feb, 2018 12:31 AM

    Ilya Finkelshteyn's Avatar

    Try to remove '', e.g. package_version: ${app_version}.{build}. Generally I would recommend to use UI and then Export YAML button to spend less time wrestling with YAML syntax.

  4. 4 Posted by jstafford on 15 Feb, 2018 12:41 AM

    jstafford's Avatar

    C:\Users\appveyor\AppData\Local\Temp\1\NuGetScratch\c4oyvlx2.w5v.nugetrestore.targets(131,5): error MSB4018: System.ArgumentException: '$app_version' is not a valid version string. "

    It seems that the variable is not hydrating. Just to make sure my environment variable was declared correctly, I did as you suggested and used the UI to generate the value:

    environment:
        app_version: 2.1.0
    
    dotnet_csproj:
        patch: true
        file: '**\*.csproj'
        version: ${app_version}
        package_version: ${app_version}.{build}
        file_version: ${app_version}.{build}
        assembly_version: '1.0.0'
    
  5. 5 Posted by Ilya Finkelshte... on 15 Feb, 2018 01:03 AM

    Ilya Finkelshteyn's Avatar

    You found a bug :) https://github.com/appveyor/ci/issues/2078

    Use $(variable) or %variable%. %variable% will require '' in YAML.

  6. 6 Posted by jstafford on 15 Feb, 2018 01:28 AM

    jstafford's Avatar

    Ok thanks. So that resolved the error but now it is not filling in the Build number

    I've tried:

    • $(app_version).{build}
    • $(app_version.(build)
    • $(app_version).$(build)
    • $(app_version).$(APPVEYOR_BUILD_NUMBER)

    and all are naming the nuget package 2.1.0 instead of 2.1.0.410

  7. 7 Posted by Ilya Finkelshte... on 15 Feb, 2018 01:55 AM

    Ilya Finkelshteyn's Avatar

    $(app_version).{build} and '%app_version%.{build}' should work. If not, send the whole YAML and build log.

  8. 8 Posted by jstafford on 15 Feb, 2018 04:21 PM

    jstafford's Avatar

    Ok I figured it out. I didn't have in the csproj so it was getting skipped, I think. Once I put it in, the versions came out correct. Thanks!

  9. 9 Posted by Ilya Finkelshte... on 15 Feb, 2018 07:47 PM

    Ilya Finkelshteyn's Avatar

    Right, here is issue about it. We finally decided that it is OK if AppVeyor will add Version and others if they do not exist.

  10. 10 Posted by jstafford on 15 Feb, 2018 08:24 PM

    jstafford's Avatar

    Great. Thanks Ilya. You're a superstar.

  11. 11 Posted by Ilya Finkelshte... on 15 Feb, 2018 08:34 PM

    Ilya Finkelshteyn's Avatar

    Shhh, don't tell anyone :)

  12. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:27 AM.

Comments are currently closed for this discussion. You can start a new one.

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