The main question was…How to start a build when another build succeeds in TFS. In other words...how to invoke a TFS build from another TFS build?
I don’t need to use any third-party library to do so, all I had to do is to edit the default Lab Management process temple which already does that (invoke a build from other build).
I copied the build process and removed most of the activities, variables and arguments and made some modifications. The following is an image of all the processes and I will explain what happened in each section:
Here is the build definition that uses the process. It only has 4 arguments. We can see that build definitions arguments is a list of strings so we can point to many builds as needed, also we have the option to run build in sequence if builds depend on each other or disable that in case there are no dependencies.
In the following section, I just added a loop (For Each) that iterate over the list of given builds and invoke each one, note here that I check if we want run the build in sequence, see the next step.
If we want to run the build in a sequence (the condition is true) to make each build depend on the previous build's status and if one build fails then the main build will stop and fail.
To do so, I waited for the build untill it was finished and changed the main build status (success or fail). If it failed then the main build would stop (ThrowOnError) and fail, if it succeeded, it would go to the next iteration in the loop (wait for the next build).
If we don’t want to run the build in sequence (condition is false) we just need to trigger all builds with no dependencies, that means the main build always succeeds even if other child builds fail, because it doesn't depend on them. Even the main build may finish while other build still running. I made the main build wait for the last triggered build in the loop, but if the last build finished before the previous one, the main build will finish before all builds finish. So, I also changed (ThrowOnError) to be false.