Use Bamboo variable for condition in MSBuild file
Asked by Stan Wijckmans:

Hello all!

<br />

I'm trying to set a constant in an MSBuild file depending on a Bamboo variable, but for some reason MSBuild is not picking it up.  
My MSBuild file looks like this:

```
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BuildBinaries">  <Target Name="BuildBinaries">    <MSBuild Projects="..\MySolution.sln" Targets="Build" Properties="Configuration=Release;DebugType=None;Constants=" BuildInParallel="true" Condition="'$(bamboo_ProduceReleasableBuild)' == 'False'"/>    <MSBuild Projects="..\MySolution.sln" Targets="Build" Properties="Configuration=Release;DebugType=None;Constants=PRODUCE_RELEASABLE_BUILD" BuildInParallel="true" Condition="'$(bamboo_ProduceReleasableBuild)' != 'False'"/>  </Target></Project
```

I also have a plan variable named ProduceReleasableBuild.  

The problem I'm having is that MSBuild always runs the task with condition bamboo_ProduceReleasableBuild == 'False', never the !false one. When I check with a powershell script I can see that Bamboo is setting the bamboo_ProduceReleasableBuild env variable correctly, also when I override it for a custom build.  

I tried this on my local machine with an environment variable bamboo_ProduceReleasableBuild set and this behaves correctly for both the False and !False cases.  

Any idea what the issue could be?  
The Bamboo version is 5.8.1.

<br />

Regards

Stan


Answers:
author: Stan Wijckmans
body: Update 3 and last update:

I have currently solved it by moving the Condition part to DefineConstants element in the csproj file. Still no idea what the issue was with having it in the build xml though.


author: Stan Wijckmans
body: Update 2 for today: It turns out to be not related to the variables, so we can stop looking at that.

I just changed my build xml to:

```
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BuildBinaries">
? <Target Name="BuildBinaries">
?<MSBuild Projects="..\MySolution.sln" Targets="Build" Properties="Configuration=Release;DebugType=None;Constants=PRODUCE_RELEASABLE_BUILD" BuildInParallel="true"/>
? </Target>
</Project>
```

to make it always produce a releasable build.

When running on my local machine my #if PRODUCE_RELEASABLE_BUILD check in code works, when it's build through bamboo it doesn't. In the logs it's visibile that csc does define it though. Very strange...


author: Stan Wijckmans
body: Hello, first of all my apologies for the very late reply.

I finally had time to check some more things on our build machine.

Currently I have the following build xml:

```
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BuildBinaries">
? <Target Name="BuildBinaries">
?<MSBuild Projects="..\MySolution.sln" Targets="Build" Properties="Configuration=Release;DebugType=None;Constants=PRODUCE_RELEASABLE_BUILD" BuildInParallel="true" Condition="'$(bamboo_ProduceReleasableBuild)' == 'true'"/>
??? <MSBuild Projects="..\MySolution.sln" Targets="Build" Properties="Configuration=Release;DebugType=None;Constants=" BuildInParallel="true" Condition="'$(bamboo_ProduceReleasableBuild)' != 'true'"/>
? </Target>
</Project>
```

I checked my local machine scenario also on the build machine and there it also behaves correctly:

I copied the source code to the desktop of the build machine and created a system environment variable named bamboo_ProduceReleasableBuild and set it to true. It builds with Constants=PRODUCE_RELEASABLE_BUILD, when it is not set to true it builds without this constant. This is what I want.  
Afterwards I removed the system environment variable and even restarted the machine to ensure there are no leftovers from this system variable.  
When I build the same source code through bamboo (so source code on bitbucket) and trigger a custom build with this variable set to true it does not build with the Constants set.  

[Bamboo log](https://1drv.ms/u/s!AhqurxiexJWrifgLx_YFB_eTfuIHGQ) you can see the bamboo_ProduceReleasableBuild is set to true here.  

Unfortunately I cannot share the full source code or our project.


author: Stan Wijckmans
body: Hmmmm it appears to be an issue with MSBuild rather than with bamboo variables. It seems to ignore custom parameters that are passed to it on our build machine. I'll investigate this further during the weekend and get back to you when I know more.

comments: author: somethingblue
body: Hi Stan,

No worries. I'll investigate further on this end as well and let you know if I come with anything in the meantime. If you do figure it out before I get back to you please post what you found so we can all learn something!

Cheers,

Branden


author: somethingblue
body: Hi Stan,

You may want to check the syntax and see if this works:

Variables can be used in all fields of a task or deployment, with the exception of password fields. Use the following format when referencing a variable:

```
"'${bamboo_ProduceReleasableBuild}'
```

This is per the [Bamboo Variables](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html) document.

Cheers,

Branden

comments: author: Stan Wijckmans
body: I'm affraid this was not it. MSBuild documentation states that in MSBuild files you have to use normal braces and not curly ones.

As I said "'$'(bamboo_ProduceReleasableBuild)' behaves correctly for False/True settings when I create an environment variable with the name bamboo_ProduceReleasableBuild.

On our build machine with a variable named ProduceReleasableBuild it does not.


Tags: ms-build

variable