In this article, we will view various ways using which environmental variables can be accessed with Build Definition. We will be using Visual Studio 2013 and Team Foundation Server 2013 Build Service for that. Sometimes we need to use the path to the source folder or the folder where the build binaries are getting dropped. Such kind of variables are termed as TF_BUILD environment variables.
Environmental Variables are a set of values that can help in running build definitions. These are created and set by the Team Foundation Build Service before the actual build workflow is invoked. We may require these variables for various reasons. For example, we may want to use InvokeProcess activity which in turn will call another process. Our need is to send the drop location of the binaries to it. Another example can be that we want to add another section in build summary where we provide the build reason and build number. We can use environmental variable to achieve that and send it to WriteCustomSummaryInformation activity.
You will find out more information about InvokeProcess and WriteCustomSummaryInformation in my next upcoming article.
TFS 2013 Build Service supports the following environmental variables.
TF_BUILD_BINARIESDIRECTORY, TF_BUILD_BUILDDEFINITIONNAME, TF_BUILD_BUILDDIRECTORY, TF_BUILD_BUILDNUMBER, TF_BUILD_BUILDREASON, TF_BUILD_BUILDURI, TF_BUILD_COLLECTIONURI, TF_BUILD_DROPLOCATION, TF_BUILD_SOURCEGETVERSION, TF_BUILD_SOURCESDIRECTORY
All these names are self-explanatory.
We will find out 3 ways using which we can access these variables
- Using PowerShell Script for variables
- Using the variables in MSBuild by setting parameters
- Using custom build process for environmental variables
Use Environmental variables in PowerShell script
Build variables can be used in script by using the same names I have provided above. Follow this procedure to access build variables with their names in the PowerShell scripts.
Open any existing project that you want to build.
1. Add a new item to project of type text file to “PreBuildScript.ps1”
2. Add these lines as commands to the script
Write-Host 'This is pre build script'
Write-Host "The build definition name is " $Env: TF_BUILD_BUILDDEFINITIONNAME
Write-Host "Drop Location is " $Env: TF_BUILD_DROPLOCATION
Write-Host 'pre build script over'
3. Add another item named “PostBuildScript.ps1” to the project with following commands:
Write-Host 'This is coming from post build script'
Write-Host "Build Number is " $Env: TF_BUILD_BUILDNUMBER
4. Save both the files and check-in
5. Create a build definition with name [Project Name] – Run Script
6. Keep manual trigger
7. Provide the build controller name if not already selected
8. You need a shared folder on the Build Server as drop location. Provide the path to the TFS shared folder name for staging location as follows:
9. From Process tab, remove the automated tests to be executed, if you don’t want to execute any automated tests
10. Optionally provide following Build number format in Process tab 5.Advanced
11. Provide the path to pre and post build scripts and specify -verbose for verbosity as shown. We will be able to see the output of the scripts in Diagnostics Logs of build.
12. Save the build definition and queue the build
13. Once the build is successful, right click on it in Build Explorer and select Open in browser
14. Select Diagnostics tab. Scroll down until you find the following
Use Environmental variables in MS Build
When you are using MS Build activity to compile the code, you can pass the arguments to it as and when required. This can be either using a default template or a custom template. Once the parameter is available to MSBuild process, it can be accessed and used in target as required.
I am using the example of setting build number as argument
After saving the build definition and executing it, we can see the parameter appearing.
Below is the screenshot of build diagnostics without parameter passing
Using Environmental Variables with Build Process
We can pass the environmental variables to any activity in either default process or custom process. In order to achieve this, we need to use the activity named GetEnvironmentalVariable<T>
1. Create either a variable or an argument to pass the value to any activity
2. Argument can be passed as a parameter while either editing a build definition or queuing it or at both times, if set so. I am going to add these in a Sequence activity
3. I have given name as “dropLocation” to it
4. Provide the configurations to the activity as follows:
is available in assembly “Microsoft.TeamFoundation.Build.Activities.dll”
5. Observe the result is passed to the variable or argument already declared. This can be used in another activity as and when required.
6. In this case, we will add a WriteBuildMessage to show the value. It can be used as a mediator between multiple activities. The complete workflow looks as below:
7. The Diagnostics tab for the successful build will show the details of the drop location.
In this article we discussed how environmental variables can be used with Visual Studio 2013 and Team Foundation Server 2013 Build Service. I hope you enjoyed reading this article.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Purchase this eBook at a Discounted Price!