I run into a situation which needed to run a batch file from TFS Build, this batch file downloads some files from a file server and I just needed to copy those files later during the same build process to another machine. I will simulate that by creating a batch file that creates a file and copies the created file to another machine. Here is the batch file that creates a new text file:
If I just double-click on that file, it will create a new file called NewFile.txt:
- Open any Build Process Template and delete all its activities.
- Add Sequence Activity with name: Overall Build Process.
- Drag and drop GetBuildDetail Activity inside the Sequence Activity.
- Drag and drop AgentScope Activity with name: Run on Agent.
- Drag and drop GetBuildDirectory Activity inside AgentScope Activity.
- Drag and drop InvokeProcess Activity with name: Call a batch file.
- Declare a variable of type IBuildDetail with name: MyBuildDetail, and assign the return value from GetBuildDetail to it.
- Declare a variable of type String with name: MyBuildDirectory, and assign the return value from GetBuildDirectory to it.
More information on how to assign a value to a variable from the return execution of an activity.
We need to include the folder that contains the script or the batch file in the Source Settings of our Build Definition, so the build server copies it in the working directory of the build. But first, let’s remember what is the working directory of the build. It’s a folder like a temp folder the build process uses to get the last changeset so it could build it. We configure that from TFS Administration Console for the agent:
So we will need to include the folder that contains the script or the batch file in the Source Settings of our Build Definition:
So now if we run this build, we will find that it gets the last changeset including the script:
So now I can give the InvokeProcess Activity the path on the working directory as the following:
Remember if I run the build, the file will not be created in the related path, but it will be in System 32 folder:
Then I will need to copy that file to the needed machine, see the following post for typical steps: How to Copy Build output to a Lab Environment Machine for TFS2013