3

Background

Currently I run a pre-build command when building my solution locally. However, this command isn't needed when building on my VSTS Continuous Integration Server.

Question

Is there a way to only run the pre-build event commands when building on a local machine?

I'm aware you can add conditional statements like below. But is there something to distinguish if its a local build or a CI build?

if $(ConfigurationName) ==  Local 
Dan Cundy
  • 2,649
  • 2
  • 38
  • 65

1 Answers1

4

When using the Visual Studio build task in VSTS you can pass MSBuild parameters from the task configuration. This way you can define your own custom property like RunsOnCI and default it to false. You can then set it to true in your build definition.

Assume you have the following code in your pre build event:

if $(BuiltOnCI) == true (
    echo "Hello World!"
)

You then need to edit your .csproj file and add the BuiltOnCI property with a default value:

<PropertyGroup>
    ...
    <BuiltOnCI>false</BuiltOnCI>
</PropertyGroup>

You can test your changes by running MSBuild on the command line. Running it like this will not show the Hello World message:

msbuild myproject.csproj

Passing the parameter on the command line allows you to set it to true:

msbuild StackoverflowSample.csproj /p:BuiltOnCi=true

Now that it works locally, you can use /p:BuiltOnCi=true and put in your VSTS task in the MSBuild arguments field.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • Would you be able to expand on this a little more. I think I understand the logic, just not where to set all the components. I've appended `/p:BuiltOnCI=true` in my MSBuild Arguments in VSTS and set this condition in my pre-build events in my solution.`if "$(BuiltOnCI)" != true` – Dan Cundy Jan 22 '18 at 15:55
  • @DanCundy I've updated my post with a sample. Does that work? – Wouter de Kort Jan 22 '18 at 16:08
  • That worked well, thank you. Struggled a little with the pre-build syntax, but got there in the end. – Dan Cundy Jan 22 '18 at 17:17