Reading MSBuild task parameters

Hi All,

I am new to TeamCity and would like some assistance with parameters.

I have successfully been able to pass parameters from TeamCity into my MSBuild. For example, I have a parameter of system.Configuration set up where I can specify Debug or Release - and MSBuild reads this into the $(Configuration) parameter.

Now I am trying to feed data the other way. In my MSBuild task, I have the following defined:

    <GetAssemblyIdentity AssemblyFiles="$(MSBuildProjectDirectory)\bin\Release\MyAssembly.dll">
      <Output TaskParameter="Assemblies" ItemName="MyAssemblyInfo" />
    </GetAssemblyIdentity>

Within the MSBuild script I am able to use this as a task parameter (for example %(MyAssemblyInfo.Version) returns the version number of the assembly). How can I read this same version number into TeamCity so that I can use this in subsequent steps of the build process?

Thanks in Advance
Adam

4 comments
Comment actions Permalink

Hello Adam,
Welcome the awesome world of TeamCity!

TeamCity won't "Natively" read this value. What you can do however is use TeamCity service messages to echo special strings out that TeamCity will know how to interpret.

Take a look at:
http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-AddingorChangingaBuildParameter
http://msdn.microsoft.com/en-us/library/6yy0yx8d.aspx

So I think you should be able to something similar to:

<Target Name="DisplayMessages">
    <Message Text="
##teamcity[setParameter name='my.assembly.info.version' value='
%(MyAssemblyInfo.Version)
']
" />
</Target>


Apologies my MSBuild is a little rusty.

You could then reference this in any subsequent build step as %my.assembly.info.version%.

I think one thing to be careful of is if you reference this parameter it will add it into the Parameters tab with a value of <value is required>. This could stop the build running, so just edit the parameter and give it a default value of empty string or something and then it will get set during build.

I haven't tested this but I hope I have helped :).

0
Comment actions Permalink

Thanks Sean! That did exactly what I needed. I was able to create the parameter in TeamCity without filling in any default value and the value was filled in by MSBuild when the target was run.

0
Comment actions Permalink

I know this is an old topic, but it probably answers my question too. One quick question I have is how and when should my build configuration run the DisplayMessages task? Is it auto-detected by TeamCity or do I create an MsBuild task before the task that compiles my project? Also, can I use the my.assembly.info.version parameter in the Build Number Format and Artifact Paths of the General Settings?

0
Comment actions Permalink

You can use my.assembly.info.version parameter in Artifact Paths, because it resolves at the end of build, but not in Build Number Format. To set build number you need to use service message:

##teamcity[buildNumber '%my.assembly.info.version%']

0

Please sign in to leave a comment.