huserben / TfsExtensions

Extensions for TFS 2015+ such as custom Widgets (require TFS 2017) and Build Tasks
MIT License
44 stars 22 forks source link

Waiting For Build to finish with just one build agent in one queue #62

Closed huserben closed 6 years ago

huserben commented 6 years ago

Original Question by @ushastyy from https://github.com/huserben/TfsExtensions/issues/61#issuecomment-376514998

I have one BuildAgent and one queue. I want to second definition was build before first. In the first definition I have added TriggerBuild for building second. TriggerBuild adds second definition in queue, and whaits when its finishes. The second definition whaits when the first finishes to start. Deadlock. Can you please give me an advice, what should I do? Thanks.

huserben commented 6 years ago

So you have the following Situation: Build1 Build2

Build1 triggers Build2 and should wait for the completion of it? If so, yes with just one build agent this will result in a deadlock situation. It is simply not possible to achieve this with just one build agent and 2 build definitions. However there is a possibility which I described as well in this question: https://github.com/huserben/TfsExtensions/issues/52#issuecomment-370544400

The bottom line is, you have to split up your Build1 Definition into two, the first part will do everything up to (and including) triggering Build2. Build3 has to be added and it will do everything that shall be done after Build2 completed.

Now you can do the following: Build1 has to trigger Build2. Here you have to make sure to Store the TriggeredBuildIds in the variable, we use it in the next step: Build1 has to trigger as well already Build3. As parameter to this Build3 you have to pass the ID of Build2 that was triggered. You can specify this in the TriggerBuildTask under Advanced Configuration: TriggeredBuildIds: $(TriggeredBuildIds)

After this your Build1 should be finished. In the BuildQueue we now have Build2 and Build3 waiting. Build2 will run (either successful or not). After this, Build3 will be started.

The Build3 Configuration shall start with a "WaitForBuild" Task. In order to await Build2, you have to specifiy in the Variables tab the Variable "TriggeredBuildIds" and make sure to mark it as "Settable at Queue Time". This allows that when you trigger the build from Build1 the variable can be set. Now on this WaitTask you will wait for the completion of Build2. By nature this has already happened as you only have 1 agent. However you can then as well fail the build if Build2 was not successful or download artifacts etc.

I hope that cleared things up. Otherwise please let me know.

ushastyy commented 6 years ago

Hi @huserben! This approach is a little tricky, but it works! Thank you!

huserben commented 6 years ago

Yes I know, however it's the only way I see in case you are restricted to a single build agent.

If this answers your question I will therefore close this issue.

Feel free to open new Issues in case you find something that isn't working correctly, you have another question or a proposal for improving the tasks.