Gonnagle
Gonnagle

Reputation: 132

Azure DevOps - Pass MSBuild properties as parameters to other build tasks

In Azure DevOps, is it possible to export few MSBuild properties defined in MSBuild Tasks during the build task and use these as variables for the other tasks in the build job?


Use case: Version number computed during build

We have created a MSBuild task that as a part of the build determines version number from the git tag. The MSBuild task uses this to set the assembly versions as well as for the possible nuget packages packaged as a part of the build.

Now when setting up the Azure DevOps build pipeline I have a separate step for building an installer for the service (.msi using Advanced Installer). I need to get the version determined during the MSBuild task and pass it for the Advanced Installer build task, so it can be versioned accordingly. How can this be achieved?


As a workaround we've been previously using separate build task where the version has been determined using bash script. And then exported into VSO variable and passed to both tasks, MSBuild and Advanced Installer build (the MSBuild task can use predefined property for the version value). Now I'd like to get rid of the duplicate way of computing the version.

Upvotes: 1

Views: 2724

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

As long as you don't turn off msbuild's console logging, you can use logging commands inside your csproj file:

<Target Name="SetAzureDevOpsVariables" BeforeTargets="BeforeBuild">
  <Message Importance="high"
           Text="##vso[task.setvariable variable=ProductVersion]$(Version)" />
</Target>

$(Version) will be substituted by msbuild with the value of the property (you can use PackageVersion, FileVersion etc.) and the agent will interpret the command from MSBuild's output. Importance="high" will ensure that the message is printed even when the log verbosity is reduced.

Note that you can also put a Condition="'$(SYSTEM_TEAMPROJECTID)' != ''" on the <Target> element to only run this output on Azure DevOps builds.

Upvotes: 7

Related Questions