Setting and reading environment variables from build runner

Hi,

This should be simple, but I'm really not understanding how TC 5.0 build agents interact with environment variables.

Let's say I have a build runner 'MyProject'.  It's using the sln2008 runner.  I would like the build runner to set the environment variable 'VAR1' to the internal build number used by TC for storing the artifacts created from 'MyProject'.

I have a dependent build runner named 'DeployMyProject', which uses the command line runner, and is only triggered after a successful build of 'MyProject'.  I would like to use the 'VAR1' environment variable as either a parameter passed to the command line, or as a normal environment variable I can reference in a Windows batch file.

Can anyone out there tell me how to interact with environment variables?

Best Regards,

David Montgomery

7 comments
Comment actions Permalink

Given that builds may run on any agents at any time (even in parallel) the task is not so easy as it seems to be.
You can use artifact for the purpses of passing a parameter from one build to another.
Current build build number is set to environment variable called: BUILD_NUMBER

0
Comment actions Permalink

Hi Pavel,

OK, I've read the docs about teamcity-info.xml to pass parameters between build runners.  Unfortunately, it says nothing about how to actually configure a sln2008 runner to read/write from teamcity-info.xml.

How is this done?

Best Regards,

David Montgomery

0
Comment actions Permalink

I think you should use MSBuild runner for that. Solution runner by its nature can't allow such customizations, while in MSBuild runner you can have custom tasks and more flexibility.

0
Comment actions Permalink

Hi Pavel,

Can you give me an example of how I can write to teamcity-info.xml using MSBuild?  I'm specifically looking to capture the internal build number and the resolved build number of each build.

...and do you have any examples of how a dependent build will then read from teamcity-info.xml?

Best,

David

0
Comment actions Permalink

In the first build you may create an version number file by the following msbuild script:

  <PropertyGroup>
    <VersionFile>VersionFile.txt</VersionFile>
    <BUILD_NUMBER>43434</BUILD_NUMBER>
  </PropertyGroup>

  <WriteLinesToFile File="$(VersionFile)" Lines="$(BUILD_NUMBER)" Overwrite="true"/>


  <ReadLinesFromFile File="$(VersionFile)">
    <Output TaskParameter="Lines" ItemName="ItemsFromFile"/>
  </ReadLinesFromFile>

  <PropertyGroup>
    <VersionFromFile>@(ItemsFromFile)</VersionFromFile>
  </PropertyGroup>

  <Message Text="Loaded from file: $(VersionFromFile)" />



To wrap .sln file with msbuild script, use <MSBuild Project="path_to_sln.sln> task
0
Comment actions Permalink

Hi Eugene,

Thank you so much for the detailed example!

Can you tell me what the system property is for retrieving the INTERNAL build number that Team City uses for publishing to the Artifact path? (i.e. the auto-incrementing build number we can't change)

Best Regards,

David Montgomery

0
Comment actions Permalink

Do you mean build_id? Are you sure you need it? Could you please describe your case in detail.

You may try $(idea_build_server_build_id). This property is not documented and may change in next releases of TeamCity.

0

Please sign in to leave a comment.