Run a script in your build process

The Team Foundation Build (TFBuild) default template provides the core capabilities that most teams need to build and test their app. Even so, most software projects are unique enough to require teams to add their own business logic. Developing and maintaining a custom build process can be complicated and expensive. But you don’t have to do that if you just want to run a PowerShell script or a batch (.bat) file before or after the system compiles or tests your code.

For example, before you compile, you want to set the version of your assembly files. Or after you compile, you want to control the build outputs that get dropped to your staging location.

Run scripts in your build process

Script parameters on default template

You can use the default template to run PowerShell and batch (.bat) scripts before and after you compile your code, and before and after your run your tests.

Before your build process can run your script, you must first upload it:

After you upload your script, reference it from the appropriate place in the build process parameters. If necessary, you can also supply arguments to the scripts.

Pre-build example: Version your assemblies

Even though there are many tasks you might want to perform before MSBuild compiles your code, one of the most common of these might be to apply a version to your assemblies.

ApplyVersionToAssemblies.ps1

Post-build example: Prepare your binaries to be staged

When you set Output location to AsConfigured, TFBuild yields control of which binaries are dropped to you. In this case, TFBuild compiles the binaries and leaves them in the source directory, organized essentially the same way as if you used Visual Studio to compile them locally on your dev machine. The following script gathers some of the typical binary types from the typical locations and copies them to the folder from which TFBuild copies and drops to your staging location.

GatherItemsForDrop.ps1

Q & A

Q: I was blocked by the system because I don’t have permission. How do I get it?

A: Permission reference for Team Foundation Server

Q: What environment variables are available to my script?

A:Get data from TF_BUILD environment variables

Q: Where can I get more information about developing scripts?

A:Curated answer: Run scripts in your Team Foundation Build process

Q: Can I run version control commands?

A: Yes. Here’s an example that shows how to check out and check in a file:

& ($env:VS120COMNTOOLS + "..\..\common7\ide\tf.exe") checkout $file

& ($env:VS120COMNTOOLS + "..\..\common7\ide\tf.exe") checkin $file /comment:" Checked in by build process script. ***NO_CI***" /noprompt

Q: Where can I get scripts?

Community TFS Build Extensions

Q: Scripts don’t give me enough control. What can I do?

Customize your build process template.