huserben / TfsExtensions

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

Download Artifacts of original Build in triggered Build #24

Closed JaCoB1123 closed 7 years ago

JaCoB1123 commented 7 years ago

Hi,

I'm currently trying to do something like this:

Is this currently possible? If so, how?

Thanks, Jan

huserben commented 7 years ago

Hi

no this is not possible with the Task out of the box, as there is no info from where the build was triggered (and we don't know either if it even was finished or not).

However if you want you could write a script, e.g. in PowerShell to figure out the build id of the last Build A that was run, and if you then store it in the environment variable called "TriggeredBuildIds" as part of Build B you could use the wait task and fetch the artifacts.

This you could do for example as part of Build A (write the build id that should be available as environment variable already somewhere where Build B can pick it up, e.g. via text file or environment variable that exists outside of the scope of Build A) or just within Build B and query the TFS REST API yourself to fetch the last build of Build A that was run and use this build id.

However if you go for such a solution (especially the first one) I would make sure to clean the stored build id's again not that for a later build you will fetch old values.

JaCoB1123 commented 7 years ago

Ok, thanks. I might try writing a script for this use case. Would you be interested in a PR adding this?

huserben commented 7 years ago

Hi @JaCoB1123

I found a solution where you don't even need to write a script but can make use of the build parameters settings of the Trigger Build Task: In Build A where you Trigger Build B, specifiy the following additonal parameter: \"TriggeredBuildIds\": \"$(Build.BuildId)\" grafik

Then in Build B, specify TriggeredBuildIds as a variable of the build definition (don't set a value, but make sure you make it settable at queue time): grafik

This will now trigger Build B and set the variable "TriggeredBuildIds" to the id of the build that is triggering it. If you then use a Wait Task it will pick up this ID and will wait for it to finish and you can donwload the artifact.

Sorry I didn't think of this way of doing it before, I hope you did not yet spend too much time on working on some manual script of yours.

JaCoB1123 commented 7 years ago

Hi @huserben awesome! I actually thought about setting a variable in the triggered build, but didn't invest much time yet. So thanks for the solution, that's exactly what I wanted 😃